Exception in thread “AWT-EventQueue-0“ java.lang.NullPointerException: Cannot invoke “javax.swing

想要在JTextfield上面显示鼠标点击过的button上的文本,但是出现了
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException: Cannot invoke “javax.swing.JTextField.setText(String)” because “this.jt” is null
at Calculation.actionPerformed(Calculation.java:94)错误,不知道为啥,
下面是源代码

import javax.swing.;
import java.awt.
;
import java.awt.event.*;

public class Calculation extends JFrame implements ActionListener {
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19;
JTextField jt;
JFrame f;
public Calculation() {
b1 = new JButton("%");
b2 = new JButton(“CE”);
b3 = new JButton(“C”);
b4 = new JButton("<-");
b5 = new JButton(“7”);
b6 = new JButton(“8”);
b7 = new JButton(“9”);
b8 = new JButton("*");
b9 = new JButton(“4”);
b10 = new JButton(“5”);
b11 = new JButton(“6”);
b12 = new JButton("-");
b13 = new JButton(“1”);
b14 = new JButton(“2”);
b15 = new JButton(“3”);
b16 = new JButton("+");
b17 = new JButton(“0”);
b18 = new JButton (".");
b19 = new JButton("=");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
JPanel p=new JPanel();
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(4,4));
JPanel p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JTextField jt=new JTextField(20);
jt.setFont(new Font(“Roman”, Font.PLAIN, 20));
jt.setBackground(Color.white);
jt.setEditable(false);
p.setBackground(new Color(69,137,148));
p.setSize(400,300);
p.setLayout(new BorderLayout(2,2));
p.add(BorderLayout.NORTH,jt);
p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b5);p1.add(b6);p1.add(b7);
p1.add(b8);p1.add(b9);p1.add(b10);p1.add(b11);p1.add(b12);p1.add(b13);
p1.add(b14);p1.add(b15);p1.add(b16);
p.add(BorderLayout.CENTER,p1);
b17.setPreferredSize(new Dimension(200,28));
b18.setPreferredSize(new Dimension(100,28));
b19.setPreferredSize(new Dimension(100,28));
p2.add(b17);p2.add(b18);p2.add(b19);
p.add(BorderLayout.SOUTH,p2);
setContentPane§;

}

public void actionPerformed(ActionEvent e) {
	if(e.getSource() == b1){
		jt.setText(jt.getText()+e.getActionCommand());}
	else if(e.getSource() == b2){
		jt.setText("CE");}
	else if(e.getSource() == b3){
		jt.setText("c");}
	else if(e.getSource() == b4){
		jt.setText("<-");}
	else if(e.getSource() == b5){
		jt.setText("7");}
	else if(e.getSource() == b6){
		jt.setText("8");}
	else if(e.getSource() == b7){
		jt.setText("9");}
	else if(e.getSource() == b8){
		jt.setText("*");}
	else if(e.getSource() == b9){
		jt.setText("4");}
	else if(e.getSource() == b10){
		jt.setText("5");}
	else if(e.getSource() == b11){
		jt.setText("6");}
	else if(e.getSource() == b12){
		jt.setText("-");}
	else if(e.getSource() == b13){
		jt.setText("1");}
	else if(e.getSource() == b14){
		jt.setText("2");}
	else if(e.getSource() == b15){
		jt.setText("3");}
	else if(e.getSource() == b16){
		jt.setText("+");}
	else if(e.getSource() == b17){
		jt.setText("0");}
	else if(e.getSource() == b18){
		jt.setText(".");}
	else if(e.getSource() == b19){
		jt.setText("=");}
	
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Calculation f = new Calculation();
	f.setTitle("计算器");
	f.setSize(400,300);
    Image img=new ImageIcon("D:\\eclipse\\picture\\calculation.jfif").getImage();
    f.setIconImage(img);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.pack();
	f.setVisible(true);
}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 这是Java程序出现的一个错误,错误信息为“在线程”awt-eventqueue-0“中出现了异常:java.lang.NullPointerException”。它意味着程序中出现了一个空指针异常。可能是因为程序在尝试使用一个空对象引用中的属性或方法时出现了问题。 ### 回答2: 在Java编程中,当程序出现“exception in thread "awt-eventqueue-0" java.lang.nullpointerexception:”错误时,意味着程序在执行期间调用了一个空对象。 简单来说,空对象是一个未经初始化的对象,它不包含任何值,解引用时会抛出NullPointerException异常。当我们创建一个空指针时,程序会试图访问该对象上的方法或属性,但由于对象尚未初始化,因此会出现异常。 出现这种错误通常是因为程序员未正确地初始化对象,或者在程序运行中意外地删除了确切的对象。因此,为了避免出现空指针异常,我们需要确保正确地初始化和管理对象。 我们可以通过以下步骤来解决这个问题: 1. 检查代码并找出引发空指针异常的位置。 2. 检查代码中是否存在未初始化的对象。 3. 确保在创建对象后初始化、返回或赋值。 4. 使用可为空的变量时,确保在使用该对象之前对其进行空值检查。 5. 对于复杂的程序,可以使用调试器来定位问题所在。 因此,当程序出现“exception in thread "awt-eventqueue-0" java.lang.nullpointerexception:”错误时,我们需要检查代码中的对象初始化问题,并避免出现空指针。这样才能保证程序的稳定性和可靠性。 ### 回答3: 当Java程序中出现"exception in thread "awt-eventqueue-0" java.lang.nullpointerexception:"异常时,是因为程序中出现了空指针异常。所谓空指针异常,是指Java程序在运行时,尝试调用空对象的某个方法或属性,导致程序崩溃。这个问题很常见,但往往难以找到出现空指针的具体位置。 解决这个问题的方法和技巧如下: 1. 查看程序运行的日志信息,找到出现异常的文件和行数。一般来说,日志信息会告诉我们哪个文件和哪一行出现了空指针异常。 2. 使用IDE的调试工具,逐步跟踪程序的执行过程,尤其是注意变量的赋值和使用。通过这种方式,可以逐步排除问题。 3. 对于代码中可能返回空对象的情况,要谨慎地使用null判断语句。一般情况下,我们可以使用if (object != null)的方式进行判断,避免空指针异常的发生。 4. 代码中避免使用过长的链式调用,这种过于复杂的链式调用一旦出现空对象就会导致空指针异常的发生。 5. 对于程序中某些方法或函数的调用,需要考虑返回值是否为空对象,以避免出现空指针异常。 总之,空指针异常是一种常见的问题,但通过对程序的仔细调试和代码的优化,我们可以避免和解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值