由空指针异常引起的初始化程序中的异常

时间:2011-09-21 11:19:01

标签: java exception jbutton initializer

我不知道为什么会这样,但有一天它没事。现在,我尝试运行代码,然后得到ExceptionInInitializerError。这是获得它的线:

final JButton connect = new JButton("Connect");

堆栈追踪:

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at program.gui.Chat.<init>(Chat.java:44)
        at program.gui.GUI.actionPerformed(GUI.java:176)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19
95)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242
)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:8
09)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMen
uItemUI.java:850)
        at java.awt.Component.processMouseEvent(Component.java:6289)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6054)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4652)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4482)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Component.dispatchEvent(Component.java:4482)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
        at java.awt.EventQueue.access$000(EventQueue.java:85)
        at java.awt.EventQueue$1.run(EventQueue.java:603)
        at java.awt.EventQueue$1.run(EventQueue.java:601)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
ntrolContext.java:87)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
ntrolContext.java:98)
        at java.awt.EventQueue$2.run(EventQueue.java:617)
        at java.awt.EventQueue$2.run(EventQueue.java:615)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessCo
ntrolContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.NullPointerException
        at GPAMaster.locale.Locale.get(Locale.java:30)
        at GPAMaster.locale.Messages$Chat.<clinit>(Messages.java:84)
        ... 39 more

3 个答案:

答案 0 :(得分:1)

Messages.Chat的静态构造函数中似乎实际上是一个问题。查看Messages.java第84行和Locale.java第30行。这些是相关的行 - 我怀疑你的Chat GUI有点无关紧要。

如果没有看到该代码,很难确切地猜出错误,但可能就是没有为您的语言环境提供所有本地化消息。

答案 1 :(得分:0)

您可以在该堆栈跟踪的底部看到问题的字符串提示:

Caused by: java.lang.NullPointerException
GPAMaster.locale.Locale.get(Locale.java:30)
    at GPAMaster.locale.Messages$Chat.<clinit>(Messages.java:84)
    ... 39 more

所以这意味着你有一个名为Chat的类嵌套在一个名为Messages的类中,而在第84行,你调用了Locale.get(),它正在获得一个NullPointerException方法<clinit>是类Chat静态初始化程序 - 它是编译器生成的方法,包含静态方法的任何静态块或初始化程序。

如果您在第84行显示Messages.java,在第30行显示Locale.java,我们可能会提供更多帮助。

答案 2 :(得分:0)

这是异常的关键陈述

Caused by: java.lang.NullPointerException at GPAMaster.locale.Locale.get(Locale.java:30) at GPAMaster.locale.Messages$Chat.<clinit>(Messages.java:84) ... 39 more

你的构造函数之前调用了一些静态初始化代码中的Null指针......解决这个问题它应该再次起作用:)