4

有人知道我如何开始在 Java Swing 中调试此错误吗?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:282)
    at java.awt.Container.preferredSize(Container.java:1599)
    at java.awt.Container.getPreferredSize(Container.java:1584)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
    at java.awt.BorderLayout.layoutContainer(BorderLayout.java:804)
    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.validate(Container.java:1480)
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429)
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449)
    at javax.swing.plaf.synth.SynthTabbedPaneUI.setRolloverTab(SynthTabbedPaneUI.java:491)
    at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutContainer(BasicTabbedPaneUI.java:2384)
    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.validate(Container.java:1480)
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429)
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449)
    at javax.swing.plaf.synth.SynthTabbedPaneUI.setRolloverTab(SynthTabbedPaneUI.java:498)
    at javax.swing.plaf.basic.BasicTabbedPaneUI.setRolloverTab(BasicTabbedPaneUI.java:558)
    at javax.swing.plaf.basic.BasicTabbedPaneUI.access$2000(BasicTabbedPaneUI.java:37)
    at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mouseMoved(BasicTabbedPaneUI.java:3645)
    at java.awt.Component.processMouseMotionEvent(Component.java:6333)
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3285)
    at java.awt.Component.processEvent(Component.java:6057)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4251)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    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)

它在BoxLayout(line size = ...)中从这个方法中抛出

public Dimension preferredLayoutSize(Container target) {
Dimension size;
synchronized(this) {
    checkContainer(target);
    checkRequests();
    size = new Dimension(xTotal.preferred, yTotal.preferred);
}
4

4 回答 4

3

扩展布局管理器以提供同步不太可能有效。验证所有GUI 组件都是在事件分派线程上构建的。同样,使用或验证事件调度线程上的所有数据模型是否已更新。invokeLater()SwingWorker

于 2011-07-15T15:18:44.097 回答
1

从我可以看到的代码量来看,我猜您正在尝试在不使用事件调度线程的情况下使用摆动组件做一些事情..

这会导致像上面这样的丑陋异常。我在一个项目中遇到了这个问题,我意识到修复它为时已晚,所以我只是把所有东西都放在 try cat 中,并没有打印整个堆栈跟踪......

如果这确实是问题,您可能会捕获并忽略空指针异常。

最好的方法是使用事件调度线程

于 2011-07-15T15:17:18.827 回答
1

因为错误发生在 Swing “内部”(仅java*堆栈跟踪上的类),所以我会非常仔细地查看我的代码,以确保我的所有 Swing 调用都发生在EDT上。

于 2011-07-15T15:19:01.377 回答
0

如果您可以到达该行,那么 xTotal.preferred 很可能为 null,或者 yTotal.preferred 为 null。

于 2011-07-15T15:13:15.193 回答