gpt4 book ai didi

Java - 启动时出现 NullPointerException。 Swing 错误或实现错误?

转载 作者:行者123 更新时间:2023-12-02 00:48:03 24 4
gpt4 key购买 nike

我正在为一家公司开发一个应用程序,它以多种方式管理 Oracle 数据库。该程序是用 Java、NetBeans 编写的,而且它变得相当大。我不会说写得很好,但我经常回到旧的错误代码并尝试将其变为尽我所能地可读和操作。

我注意到,当我开始时,十分之一程序时,我收到以下异常:(尽管程序启动并正常工作,即使在这些时候)

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)

我假设这是一个 Swing 错误。这会是我的实现中的问题吗?为什么这种情况只有十分之一发生而不是总是发生?我可以做些什么来避免它吗?

最佳答案

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

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

关于Java - 启动时出现 NullPointerException。 Swing 错误或实现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4376858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com