一个关于异常的问题???
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGM extends JFrame{
JLabel lb_1,lb_2;
JButton bt;
JTextField tf;
int count,a,b;
int original[],guess[];
public void newi(){
for(int i=0;i <=3;i++){
original[i] = (int)(Math.random()*10);
for(int j=0;j <=i-1;j++){
if((i!=j)&&(original[i]==original[j])){
i++;
break;
}
}
}
}
public MyGM(){
Handler hdl = new Handler();
Container cnt = getContentPane();
cnt.setLayout(new FlowLayout());
JTextField tf = new JTextField(4);
cnt.add(tf);
lb_1 = new JLabel( "位置与数字都正确的数字个数: ");
lb_2 = new JLabel( "数字对但位置不对的数字个数: ");
cnt.add(lb_1);
cnt.add(lb_2);
bt = new JButton( "Play ");
bt.addActionListener(hdl);
cnt.add(bt);
pack();
setVisible(true);
}
class Handler implements ActionListener{
public void actionPerformed(ActionEvent evt){
System.out.println( "OK! ");
int num = Integer.parseInt(tf.getText());
}
}
public static void main(String [] args){
MyGM m = new MyGM();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
本来我对异常掌握的就不好,现在出现这个异常了,我一点也不知道,下面的是运行时抛出的异常,就是“int num = Integer.parseInt(tf.getText());”的原因,请帮我看看
Exception in thread "AWT-EventQueue-0 " java.lang.NullPointerException
at MyGM$Handler.actionPerformed(MyGM.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
[解决办法]
up
在构造方法中声明的JTextField tf屏蔽了类变量tf
造成类变量tf的值为null
在事件处理中调用了类变量tf(一直未被初始化),就报空指针错误了