“线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常”。我不知道我做错了什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10924281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException". I have no clue what I'm doing wrong
提问by Dylan Cruz
I am making a calculator for an AP Computer Science Final. I built the GUI in Eclipse using Jigloo, and I quickly tried to learn about Action Listeners so you can hit the buttons to make numbers appear. The problems started occurring when I started enter the actual code to make calculations. I keep getting the following error:
我正在为 AP 计算机科学决赛制作计算器。我使用 Jigloo 在 Eclipse 中构建了 GUI,我很快尝试了解 Action Listeners,以便您可以点击按钮来显示数字。当我开始输入实际代码进行计算时,问题开始出现。我不断收到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at NewJFrame.<init>(NewJFrame.java:82)
at NewJFrame.run(NewJFrame.java:73)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.accessjButton3.addActionListener(new ListenToOne());
0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I'm pretty new to Java, and I've never attempted a project like this before. I would love if you guys can help me fix this problem. Here is a link to my actual code, it won't fit in the code box for some reason: Link to code
我对 Java 很陌生,以前从未尝试过这样的项目。如果你们能帮我解决这个问题,我很乐意。这是我的实际代码的链接,由于某种原因它不适合代码框:链接到代码
回答by Russell Zahniser
You button variables aren't assigned until you call initGUI()
, which is at the bottom of your constructor. So, when you do this:
在您调用initGUI()
位于构造函数底部的之前,不会分配按钮变量。所以,当你这样做时:
null.addActionListener(new ListenToOne());
... Java sees this:
... Java 看到了这一点:
##代码##... which is obviously a problem.
......这显然是一个问题。