这个java程序怎么出现Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException错误?
importjava.awt.*;importjava.awt.event.*;//此方法不用内部类的形式来实现publicclassTestTextFieldAdd{p...
import java.awt.*;
import java.awt.event.*;
//此方法不用内部类的形式来实现
public class TestTextFieldAdd{
public static void main(String[] args){
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField tf1,tf2,tf3;
public void launchFrame(){
Frame frame = new Frame("TextField.text_Add");
TextField tf1 = new TextField(10);
TextField tf2 = new TextField(10);
TextField tf3 = new TextField(15);
Label addLable = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new Mornitor(this)); //添加监听
setLayout(new FlowLayout());
add(tf1);
add(addLable);
add(tf2);
add(b1);
add(tf3);
pack();
setVisible(true);
}
}
/**
*不使用内部类的形式
*/
class Mornitor implements ActionListener {
TextField tf1,tf2,tf3;
TFFrame tfFrame=null ;
public Mornitor(TFFrame tfFrame){ //也可以这样来调用TFFrame对象
this.tfFrame=tfFrame;
}
public Mornitor(TextField t1,TextField t2,TextField t3){
this.tf1=t1;
this.tf2=t2;
this.tf3=t3;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf1.getText());
int n2 = Integer.parseInt(tf2.getText());
System.out.println(tf1.getText());
tf3.setText(""+(n1+n2));
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Mornitor.actionPerformed(TestTextFieldAdd.java:56)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 展开
import java.awt.event.*;
//此方法不用内部类的形式来实现
public class TestTextFieldAdd{
public static void main(String[] args){
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField tf1,tf2,tf3;
public void launchFrame(){
Frame frame = new Frame("TextField.text_Add");
TextField tf1 = new TextField(10);
TextField tf2 = new TextField(10);
TextField tf3 = new TextField(15);
Label addLable = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new Mornitor(this)); //添加监听
setLayout(new FlowLayout());
add(tf1);
add(addLable);
add(tf2);
add(b1);
add(tf3);
pack();
setVisible(true);
}
}
/**
*不使用内部类的形式
*/
class Mornitor implements ActionListener {
TextField tf1,tf2,tf3;
TFFrame tfFrame=null ;
public Mornitor(TFFrame tfFrame){ //也可以这样来调用TFFrame对象
this.tfFrame=tfFrame;
}
public Mornitor(TextField t1,TextField t2,TextField t3){
this.tf1=t1;
this.tf2=t2;
this.tf3=t3;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf1.getText());
int n2 = Integer.parseInt(tf2.getText());
System.out.println(tf1.getText());
tf3.setText(""+(n1+n2));
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Mornitor.actionPerformed(TestTextFieldAdd.java:56)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 展开
4个回答
展开全部
TextField tf1,tf2,tf3;
为空了
改两个地方
import java.awt.*;
import java.awt.event.*;
//此方法不用内部类的形式来实现
public class TestTextFieldAdd {
public static void main(String[] args) {
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField tf1, tf2, tf3;
public void launchFrame() {
Frame frame = new Frame("TextField.text_Add");
tf1 = new TextField(10);//这里三句改下
tf2 = new TextField(10);
tf3 = new TextField(15);
Label addLable = new Label("+");
Button b1 = new Button("=");
setLayout(new FlowLayout());
add(tf1);
add(addLable);
add(tf2);
add(b1);
add(tf3);
pack();
b1.addActionListener(new Mornitor(this)); // 添加监听
setVisible(true);
}
}
/**
* 不使用内部类的形式
*/
class Mornitor implements ActionListener {
TextField tf1, tf2, tf3;
TFFrame tfFrame = null;
public Mornitor(TFFrame tfFrame) { // 也可以这样来调用TFFrame对象
this.tfFrame = tfFrame;
this.tf1 = this.tfFrame.tf1;//这里加几句,都是给三个TextField副职
this.tf2 = this.tfFrame.tf2;
this.tf3 = this.tfFrame.tf3;
}
public Mornitor(TextField t1, TextField t2, TextField t3) {
this.tf1 = t1;
this.tf2 = t2;
this.tf3 = t3;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf1.getText());
int n2 = Integer.parseInt(tf2.getText());
System.out.println(tf1.getText());
tf3.setText("" + (n1 + n2));
}
}
为空了
改两个地方
import java.awt.*;
import java.awt.event.*;
//此方法不用内部类的形式来实现
public class TestTextFieldAdd {
public static void main(String[] args) {
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField tf1, tf2, tf3;
public void launchFrame() {
Frame frame = new Frame("TextField.text_Add");
tf1 = new TextField(10);//这里三句改下
tf2 = new TextField(10);
tf3 = new TextField(15);
Label addLable = new Label("+");
Button b1 = new Button("=");
setLayout(new FlowLayout());
add(tf1);
add(addLable);
add(tf2);
add(b1);
add(tf3);
pack();
b1.addActionListener(new Mornitor(this)); // 添加监听
setVisible(true);
}
}
/**
* 不使用内部类的形式
*/
class Mornitor implements ActionListener {
TextField tf1, tf2, tf3;
TFFrame tfFrame = null;
public Mornitor(TFFrame tfFrame) { // 也可以这样来调用TFFrame对象
this.tfFrame = tfFrame;
this.tf1 = this.tfFrame.tf1;//这里加几句,都是给三个TextField副职
this.tf2 = this.tfFrame.tf2;
this.tf3 = this.tfFrame.tf3;
}
public Mornitor(TextField t1, TextField t2, TextField t3) {
this.tf1 = t1;
this.tf2 = t2;
this.tf3 = t3;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf1.getText());
int n2 = Integer.parseInt(tf2.getText());
System.out.println(tf1.getText());
tf3.setText("" + (n1 + n2));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import java.awt.event.*;
//此方法不用内部类的形式来实现
public class TestTextFieldAdd{
public static void main(String[] args){
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField tf1,tf2,tf3;
public void launchFrame(){
Frame frame = new Frame("TextField.text_Add");
tf1 = new TextField(10);//这里tf1tf2tf3不需要在声明否则变成了方法
tf2 = new TextField(10);//内成员变量
tf3 = new TextField(15);
Label addLable = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new Mornitor(this)); //添加监听
setLayout(new FlowLayout());
add(tf1);
add(addLable);
add(tf2);
add(b1);
add(tf3);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
}
/**
*不使用内部类的形式
*/
class Mornitor implements ActionListener {
//TextField tf1,tf2,tf3;
TFFrame tfFrame=null ;
public Mornitor(TFFrame tfFrame){ //也可以这样来调用TFFrame对象
this.tfFrame=tfFrame;
}
/*public Mornitor(TextField t1,TextField t2,TextField t3){
this.tf1=t1;
this.tf2=t2;
this.tf3=t3;
}*/
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tfFrame.tf1.getText());
int n2 = Integer.parseInt(tfFrame.tf2.getText());
System.out.println(tfFrame.tf1.getText());
tfFrame.tf3.setText(""+(n1+n2));
}
}
//以上是我的代码 你的错误在actionPerformed中tf1,tf2啥都没有你怎么得到数字
import java.awt.event.*;
//此方法不用内部类的形式来实现
public class TestTextFieldAdd{
public static void main(String[] args){
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField tf1,tf2,tf3;
public void launchFrame(){
Frame frame = new Frame("TextField.text_Add");
tf1 = new TextField(10);//这里tf1tf2tf3不需要在声明否则变成了方法
tf2 = new TextField(10);//内成员变量
tf3 = new TextField(15);
Label addLable = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new Mornitor(this)); //添加监听
setLayout(new FlowLayout());
add(tf1);
add(addLable);
add(tf2);
add(b1);
add(tf3);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
}
/**
*不使用内部类的形式
*/
class Mornitor implements ActionListener {
//TextField tf1,tf2,tf3;
TFFrame tfFrame=null ;
public Mornitor(TFFrame tfFrame){ //也可以这样来调用TFFrame对象
this.tfFrame=tfFrame;
}
/*public Mornitor(TextField t1,TextField t2,TextField t3){
this.tf1=t1;
this.tf2=t2;
this.tf3=t3;
}*/
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tfFrame.tf1.getText());
int n2 = Integer.parseInt(tfFrame.tf2.getText());
System.out.println(tfFrame.tf1.getText());
tfFrame.tf3.setText(""+(n1+n2));
}
}
//以上是我的代码 你的错误在actionPerformed中tf1,tf2啥都没有你怎么得到数字
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上正解。。lz要搞清楚field和local variable的区别啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询