为什么会出现nullpointexception 错误?

234234orz 2011-04-09 10:17:24
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)
...全文
223 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
风影萧诺 2011-04-11
  • 打赏
  • 举报
回复
这个JAVA常见的异常,也是最好处理的异常,用Debug模式追踪一下就知道了。。。
蓝生魂 2011-04-11
  • 打赏
  • 举报
回复
lz。。。对象已经传过来了。。你自己在new monitor的时候本就没有传FFrame对象好不。。你自己看下你的代码。。而我写的代码你试了下没
234234orz 2011-04-10
  • 打赏
  • 举报
回复
对啊 运行的时候提示空指针异常
SplM_LiS 2011-04-10
  • 打赏
  • 举报
回复
提示空指针异常?
234234orz 2011-04-10
  • 打赏
  • 举报
回复
跪请解决????? 哎呀
234234orz 2011-04-10
  • 打赏
  • 举报
回复
还是不行 每当点击等号时,都会跳出异常。将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;
}

同样会跳出异常
serbry0033 2011-04-10
  • 打赏
  • 举报
回复

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并没有被赋值 所以才会报空指针的错误
Dead_Cicle 2011-04-10
  • 打赏
  • 举报
回复
空指针异常,总的来说就是你没赋值。至于是哪部你没new出对象来,或者读文件的对象没序列化,读出来都是一个null.所以还是赋值问题。
234234orz 2011-04-10
  • 打赏
  • 举报
回复
那不是没对象传过来么
蓝生魂 2011-04-10
  • 打赏
  • 举报
回复
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();
}

这样就对了。。。楼主,求分!!!!谢谢

Jeelon 2011-04-09
  • 打赏
  • 举报
回复


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;
}


试试
WPooh 2011-04-09
  • 打赏
  • 举报
回复
你在class monitor中设置了带参数的构造函数,为何在btnequal.addActionListener(new monitor());语句中又有一个无参的构造函数?
我的电脑到没返回nullpointexception,返回的是:
TestMath.java:18: 找不到符号
符号: 构造函数 monitor()
位置: 类 monitor
btnequal.addActionListener(new monitor());
gentleboy2009 2011-04-09
  • 打赏
  • 举报
回复
应该是这行抛出的异常
public monitor(FFrame tf) {
gentleboy2009 2011-04-09
  • 打赏
  • 举报
回复
FFrame tf = null;
这个tf是null啊

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧