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)