“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException”。我不知道我做错了什么

时间:2012-06-07 00:47:13

标签: java swing nullpointerexception actionlistener listeners

我正在为AP计算机科学决赛制作计算器。我使用Jigloo在Eclipse中构建了GUI,我很快就尝试了解Action Listeners,这样你就可以点击按钮来显示数字。当我开始输入实际代码进行计算时,问题就开始出现了。我一直收到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at NewJFrame.<init>(NewJFrame.java:82)
at NewJFrame$1.run(NewJFrame.java:73)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.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)

我对Java很陌生,我以前从未尝试过这样的项目。我很乐意,如果你们能帮助我解决这个问题。这是我的实际代码的链接,由于某种原因它不适合代码框:Link to code

1 个答案:

答案 0 :(得分:6)

在调用构造函数底部的initGUI()之前,不会分配按钮变量。所以,当你这样做时:

jButton3.addActionListener(new ListenToOne());

...... Java看到了这个:

null.addActionListener(new ListenToOne());

......这显然是一个问题。