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)
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)
这个tf是null啊
public monitor(FFrame tf) {
我的电脑到没返回nullpointexception,返回的是:
TestMath.java:18: 找不到符号
符号: 构造函数 monitor()
位置: 类 monitor
btnequal.addActionListener(new monitor());
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;
}
试试
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;
}同样会跳出异常
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();
}这样就对了楼主,求分!!!!谢谢
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并没有被赋值 所以才会报空指针的错误