import java.awt.*;
import java.awt.event.*;
public class TestMath {
public static void main(String args[]) {
new FFrame().Text();
}
}
class FFrame extends Frame {
TextField num1,num2,num3;
public void Text() {
TextField num1 = new TextField(20);
TextField num2 = new TextField(20);
TextField num3 = new TextField(21);
Label pluschar = new Label("+");
Button btnequal = new Button("=");
btnequal.addActionListener(new monitor());
setLayout(new FlowLayout());
add(num1);
add(pluschar);
add(num2);
add(btnequal);
add(num3);
pack();
setVisible(true);
}
}
class monitor implements ActionListener {
            FFrame tf = null;
          public monitor(FFrame tf) {
this.tf = tf;
}

public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
//tf.num1.setText(""); tf.num2.setText("");
}
}    
为什么会出现下面的错误?  求原因   谢谢
java.lang.NullPointerException
        at FFrame$monitor.actionPerformed(TestMath.java:30)
        at java.awt.Button.processActionEvent(Button.java:388)
        at java.awt.Button.processEvent(Button.java:356)
        at java.awt.Component.dispatchEventImpl(Component.java:3931)
        at java.awt.Component.dispatchEvent(Component.java:3779)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

解决方案 »

  1.   

    FFrame tf = null;
    这个tf是null啊
      

  2.   

    应该是这行抛出的异常
     public monitor(FFrame tf) {
      

  3.   

    你在class monitor中设置了带参数的构造函数,为何在btnequal.addActionListener(new monitor());语句中又有一个无参的构造函数?
    我的电脑到没返回nullpointexception,返回的是:
    TestMath.java:18: 找不到符号
    符号: 构造函数 monitor()
    位置: 类 monitor
    btnequal.addActionListener(new monitor());
      

  4.   

    class monitor implements ActionListener {
      FFrame tf = null;
      public monitor(FFrame tf) {
    this.tf = tf;
    }改成 
    class monitor implements ActionListener {
      FFrame tf = new FFrame();
      public monitor(FFrame tf) {
    this.tf = tf;
    }
    试试
      

  5.   

    还是不行  每当点击等号时,都会跳出异常。将btnequal.addActionListener(new monitor());改成btnequal.addActionListener(new monitor(this)); 但是:在执行是还是会跳出异常 如果将class monitor implements ActionListener {
      FFrame tf = null;
      public monitor(FFrame tf) {
    this.tf = tf;
    }改成
    class monitor implements ActionListener {
      FFrame tf = new FFrame();
      public monitor(FFrame tf) {
    this.tf = tf;
    }同样会跳出异常
      

  6.   

    zl...你注意下,你在monitor类中的actionPerformed()方法中
    int n1 = Integer.parseInt(tf.num1.getText());
    int n2 = Integer.parseInt(tf.num2.getText());
    用tf对象去调用num1和num2属性,而你的tf对象却没实例化,在你的monitory构造函数中
    改成这样
    class monitor implements ActionListener {
      FFrame tf =  null; 
      public monitor() {
         this.tf = new FFrame();
      }这样就对了楼主,求分!!!!谢谢
      

  7.   

    空指针异常,总的来说就是你没赋值。至于是哪部你没new出对象来,或者读文件的对象没序列化,读出来都是一个null.所以还是赋值问题。
      

  8.   


    import java.awt.*;
    import java.awt.event.*;public class TestMath {
    public static void main(String args[]) {
    new FFrame().Text();
    }
    }class FFrame extends Frame {
    TextField num1, num2, num3; public void Text() {
    num1 = new TextField(20);
    num2 = new TextField(20);
    num3 = new TextField(21);
    Label pluschar = new Label("+");
    Button btnequal = new Button("=");
    setLayout(new FlowLayout());
    add(num1);
    add(pluschar);
    add(num2);
    btnequal.addActionListener(new monitor(this));
    add(btnequal);
    add(num3);
    pack();
    setVisible(true);
    }
    }class monitor implements ActionListener {
    FFrame tf = null; public monitor(FFrame tf) {
    this.tf = tf;
    } public void actionPerformed(ActionEvent e) {
    int n1 = Integer.parseInt(tf.num1.getText());
    int n2 = Integer.parseInt(tf.num2.getText());
    tf.num3.setText("" + (n1 + n2));
    // tf.num1.setText(""); tf.num2.setText("");
    }
    }text()方法中重新定义了num1,num2,num3 而你后面tf.num1并没有被赋值 所以才会报空指针的错误
      

  9.   

    lz对象已经传过来了。。你自己在new monitor的时候本就没有传FFrame对象好不。。你自己看下你的代码。。而我写的代码你试了下没
      

  10.   

    这个JAVA常见的异常,也是最好处理的异常,用Debug模式追踪一下就知道了