运行多个 Action 事件时出现 NullPointerException
代码用途:创建两个按钮(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信你的框架对象是空的。它永远不会被初始化。您可以读取运行时异常。它说
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
frame
未初始化,因此它解析为null
并且您无法调用null
对象上的方法。就像初始化button1
和button2
一样,您也应该初始化frame
。frame
is not initialized, so it resolves tonull
and you can't call methods onnull
objects. Like you initializedbutton1
andbutton2
you should also initializeframe
.但是
代码行在哪里呢?由于框架为空,所以什么也不会发生,我想......
But where is the
line of code ? Since frame is null, nothing can happen, I guess ...
首次使用前初始化框架:
Initialize frame before first using:
考虑重命名您的类以遵循 Java 类名称约定:CamelCase (http://en.wikipedia.org/wiki /CamelCase)
正如其他用户指出的那样,问题是框架对象没有被初始化。
在第一次使用框架对象之前添加以下行:
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: