2011-07-15 59 views
4

沒有任何人有一個想法,我怎麼能開始調試中的Java Swing這個錯誤?的NullPointerException在BoxLayout的

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(線size = ...

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

我們可以看到一些代碼 – RMT

+0

凡xTotal和YTOTAL初始化? – RMT

+0

這個錯誤是從GUI啓動或當你切換... RTM +1 – mKorbel

回答

3

擴展布局管理器提供同步是不可能有效。驗證全部 GUI組件是在the event dispatch thread上構建的。同樣,驗證所有數據模型更新的the event dispatch thread,即使用invokeLater()SwingWorker

+0

我想我會在添加組件或編輯組件時使用'invokeLater',而不是在更新模型時使用'invokeLater'。我認爲這不是必需的。 –

+0

「需要注意的是它的安全,從工藝方法內部發生變異的TableModel的,因爲它是在_Event調度Thread_調用。」 - [SwingWorker類](http://download.oracle.com/javase/6/docs/api/javax /swing/SwingWorker.html) – trashgod

+0

trashgod just revalidate()+ repaint();重繪會默認創建EDT,不是這樣,但是這個線程對我來說不是很清楚,OPs和每個帖子都是黑暗的鏡頭,因爲不可見的容器總是返回null,只是getPrefferedSize可以返回那個,我的視圖對你沒有任何意義善良的人:-)替代+1 – mKorbel

0

如果你能得到該行的話,那很可能是xTotal.preferred爲空,或者yTotal.preferred是空值。

+0

顯然:p但這並沒有真正的幫助。 –

+0

你問過如何開始調試......這是一個開始:)我要做的是在那裏設置一個斷點,然後檢查這些變量。如果其中任何一個爲空,那麼您需要追溯到初始化的位置並找出原因。 – 2011-07-15 15:37:53

1

從代碼,我所看到的,我猜你正在嘗試做擺動coponents的東西,而使用event dispatch thread量..

這將導致異常難看像上面的。我在一個項目有這個問題,我意識到它太晚能夠解決它,所以我只是把每一件事情中嘗試的貓,並沒有顯示整個堆棧跟蹤...

如果這確實是你的問題都不可能趕上並忽略空指針異常..

最好的辦法是使用事件分派線程

+0

P.S我相信你的代碼可能仍然fucntional和維度可能畢竟只要異常不停止執行創建......我可能是完全錯誤的,以及:) –

+0

+1 EDT;沒有它,秩序是不可靠的。 – trashgod

1

因爲錯誤發生在「Swing」(堆棧跟蹤中只有java*類),所以我會仔細看看我的代碼,以確保我所有的Swing調用都發生在EDT上。