运行多个 Action 事件时出现 NullPointerException

发布于 2024-09-09 05:24:46 字数 1360 浏览 7 评论 0原文

代码用途:创建两个按钮(button1和button2)。当用户单击button1时,更改button2的文本。当用户单击button2时,更改button1的文本。

这是我正在使用的代码:

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

public class multiAL {
JButton button1;
JButton button2;
JFrame frame;
public static void main(String[] args) {
    multiAL setterAL = new multiAL();
    setterAL.go();
}

public void go() {
    button1 =  new JButton("Click me, I'm One");
    button2 =  new JButton("Click me, I'm Two");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(BorderLayout.WEST, button1);
    frame.getContentPane().add(BorderLayout.EAST, button2);
    frame.setVisible(true);
    button1.addActionListener(new b1L());
    button2.addActionListener(new b2L());
}

class b1L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button2.setText("What??, you clicked 1??");
    }
}

class b2L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button1.setText("What??, you clicked 2??");
    }
  }
}

它可以完美编译,但是当我运行它时,我收到以下错误: 线程“main”中的异常 java.lang.NullPointerException 在 multiAL.go(multiAL.java:17) at multiAL.main(multiAL.java:11)

到目前为止,我只遇到了编译时错误。所以我想问两个问题:

1)代码有什么问题? 2)如何追踪运行时错误?

Purpose of the code : Create two Buttons(button1 and button2). When User clicks button1, change the text of button2. When User clicks button2, change the text of button1.

Here's the code I'm using :

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

public class multiAL {
JButton button1;
JButton button2;
JFrame frame;
public static void main(String[] args) {
    multiAL setterAL = new multiAL();
    setterAL.go();
}

public void go() {
    button1 =  new JButton("Click me, I'm One");
    button2 =  new JButton("Click me, I'm Two");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(BorderLayout.WEST, button1);
    frame.getContentPane().add(BorderLayout.EAST, button2);
    frame.setVisible(true);
    button1.addActionListener(new b1L());
    button2.addActionListener(new b2L());
}

class b1L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button2.setText("What??, you clicked 1??");
    }
}

class b2L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button1.setText("What??, you clicked 2??");
    }
  }
}

It compiles perfectly, but when I run it I receive following error :
Exception in thread "main" java.lang.NullPointerException
at multiAL.go(multiAL.java:17)
at multiAL.main(multiAL.java:11)

Till now, I've encountered only compile-time errors. So there are two question which I want to ask:

1) What's wrong with the code?
2) How to track down runtime errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月染过的梦 2024-09-16 05:24:47

我相信你的框架对象是空的。它永远不会被初始化。您可以读取运行时异常。它说 multiAL.java:17

这意味着在第 17 行你会得到 NullpointerException

I believe your frame object is null. It is never initialized. You can read the runtime exception. It says multiAL.java:17

This means that in line 17 you get your NullpointerException

记忆で 2024-09-16 05:24:47

frame 未初始化,因此它解析为 null 并且您无法调用 null 对象上的方法。就像初始化 button1button2 一样,您也应该初始化 frame

frame = new JFrame();

frame is not initialized, so it resolves to null and you can't call methods on null objects. Like you initialized button1 and button2 you should also initialize frame.

frame = new JFrame();
空袭的梦i 2024-09-16 05:24:47

但是

frame = new JFrame();

代码行在哪里呢?由于框架为空,所以什么也不会发生,我想......

But where is the

frame = new JFrame();

line of code ? Since frame is null, nothing can happen, I guess ...

尐偏执 2024-09-16 05:24:47

首次使用前初始化框架:

frame = new JFrame();

Initialize frame before first using:

frame = new JFrame();
眼前雾蒙蒙 2024-09-16 05:24:47

考虑重命名您的类以遵循 Java 类名称约定:CamelCase (http://en.wikipedia.org/wiki /CamelCase

正如其他用户指出的那样,问题是框架对象没有被初始化。

在第一次使用框架对象之前添加以下行:

frame = new JFrame();
frame.setSize(500,500);

Consider renaming your class to follow Java class name conventions: CamelCase (http://en.wikipedia.org/wiki/CamelCase)

And for the problem, as pointed by other users, is the frame object not being initialized.

Add the following line BEFORE the first use of the frame object:

frame = new JFrame();
frame.setSize(500,500);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文