Java - 开始时的NullPointerException。 Swing bug还是错误的实现?

时间:2010-12-07 13:03:35

标签: java swing nullpointerexception

我正在为公司开发一个应用程序, 它以各种方式管理oracle数据库。 该程序使用Java,NetBeans,它变得非常大。 我不会说它写得很好,但我经常这样写 回到旧的坏代码,并尝试使其成为 我可以阅读和尽可能的。

当我开始时,我注意到10次中有1次 程序,我得到以下例外:(虽然 程序启动并正常运行,即使这些时间也是如此)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.plaf.synth.SynthLabelUI.getMinimumSize(SynthLabelUI.java:199)
    at javax.swing.JComponent.getMinimumSize(JComponent.java:1702)
    at javax.swing.GroupLayout$ComponentSpring.calculateNonlinkedMinimumSize(GroupLayout.java:2933)
    at javax.swing.GroupLayout$ComponentSpring.calculateMinimumSize(GroupLayout.java:2897)
    at javax.swing.GroupLayout$Spring.getMinimumSize(GroupLayout.java:1315)
    at javax.swing.GroupLayout$ComponentSpring.calculatePreferredSize(GroupLayout.java:2904)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1593)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1590)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1593)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1590)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1590)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1588)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout.calculateAutopadding(GroupLayout.java:1050)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1037)
    at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:857)
    at java.awt.Container.preferredSize(Container.java:1599)
    at java.awt.Container.getPreferredSize(Container.java:1584)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1624)
    at javax.swing.GroupLayout$ComponentSpring.calculateNonlinkedPreferredSize(GroupLayout.java:2944)
    at javax.swing.GroupLayout$ComponentSpring.calculateNonlinkedMinimumSize(GroupLayout.java:2930)
    at javax.swing.GroupLayout$ComponentSpring.calculateMinimumSize(GroupLayout.java:2897)
    at javax.swing.GroupLayout$Spring.getMinimumSize(GroupLayout.java:1315)
    at javax.swing.GroupLayout$ComponentSpring.calculatePreferredSize(GroupLayout.java:2904)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1593)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1588)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1588)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$SequentialGroup.setValidSize(GroupLayout.java:1972)
    at javax.swing.GroupLayout$Group.setSize(GroupLayout.java:1553)
    at javax.swing.GroupLayout.calculateAutopadding(GroupLayout.java:1058)
    at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:897)
    at java.awt.Container.layout(Container.java:1421)
    at java.awt.Container.doLayout(Container.java:1410)
    at java.awt.Container.validateTree(Container.java:1507)
    at java.awt.Container.validateTree(Container.java:1513)
    at java.awt.Container.validateTree(Container.java:1513)
    at java.awt.Container.validateTree(Container.java:1513)
    at java.awt.Container.validateTree(Container.java:1513)
    at java.awt.Container.validateTree(Container.java:1513)
    at java.awt.Container.validate(Container.java:1480)
    at java.awt.Window.dispatchEventImpl(Window.java:2473)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.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)

我认为这是一个摇摆虫。这可能是我实施中的问题吗? 为什么它会在10次中发生1次而不是总是? 我可以做些什么来避免它吗?

3 个答案:

答案 0 :(得分:2)

十分之一的声音听起来可能是某个地方的竞争条件。正如@willcodejavaforfood所提到的,您需要确保所有GUI更改(包括启动)都在事件派发线程(EDT)上完成。

您可以使用SwingUtilities.invokeAndWaitSwingUtilities.invokeLater来确保代码在EDT上执行。

答案 1 :(得分:1)

SynthDefaultLookup.java 第27行,有:

public Object getDefault(JComponent c, ComponentUI ui, String key) {
    if (!(ui instanceof SynthUI)) {
        Object value = super.getDefault(c, ui, key);
        return value;
    }
    SynthContext context = ((SynthUI)ui).getContext(c);
    Object value = context.getStyle().get(context, key); // line 27
    context.dispose();
    return value;
}

...也许是componentUI的上下文?你有一些没有ComponentUI的特殊用户界面吗?

(好吧,这不是答案,我道歉,但也不是评论)

答案 2 :(得分:0)

看起来UI正在提供NullPointerException,但它不仅仅是存在而且已经结束。 UI正在使用的其他东西不能正常工作或类似的东西。你能在这里发布整个错误以及导致它的代码片段吗?