5

Does anybody have an idea how I could start debugging this error in 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)

It is thrown from this method in BoxLayout (line size = ...)

public Dimension preferredLayoutSize(Container target) {
Dimension size;
synchronized(this) {
    checkContainer(target);
    checkRequests();
    size = new Dimension(xTotal.preferred, yTotal.preferred);
}
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

4 Answers4

4

Extending a layout manager to provide synchronization is unlikely to be effective. Verify that all GUI components are constructed on the the event dispatch thread. Likewise, verify that all data models are updated on the the event dispatch thread, using either invokeLater() or SwingWorker. As failures can be obscure, this Q&A posed by @mKorbel examines ways to find EDT violations programmatically.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I think I use `invokeLater` whenever I'm adding components or editing them, but not when updating models. I thought it wasn't needed there. – Bart van Heukelom Jul 15 '11 at 15:22
  • "Note that it safe to mutate the tableModel from inside the process method because it is invoked on the _Event Dispatch Thread_."—[SwingWorker](http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html) – trashgod Jul 15 '11 at 15:32
  • trashgod just revalidate() + repaint(); repaint will create EDT by default, isn't it, but this thread isn't clear for me, OPs and every posts are shots to the dark, because unvisible container always return null, just getPrefferedSize can returns that, my view nothing to your kind person :-) for alternative +1 – mKorbel Jul 15 '11 at 15:45
  • @mKorbel: Thanks for commenting; no offense taken. @Bart: Yes, models fire events to update the corresponding view, and that _must_ happen on the EDT. – trashgod Jul 15 '11 at 20:35
  • Well I found it, though it was non-obvious. First I moved everything to the EDT, which didn't help. Then I found out that my custom list model (for a list inside a `BoxLayout` container) threw an `ArrayIndexOutOfBoundsException` in `getElementAt` (I extended `DefaultListModel`, but should've extended `AbstractListModel` instead) which Swing apparently couldn't cope with, and which caused the errors further down the line. – Bart van Heukelom Jul 18 '11 at 11:17
  • Glad you fond it! See also this [answer](http://stackoverflow.com/questions/6683952/java-swing-jmenubar-sometimes-doesnt-show-up/6686168#6686168) on uncaught exceptions and this [answer](http://stackoverflow.com/questions/6728025/how-do-i-turn-a-java-dequet-into-a-defaultlistmodel/6729087#6729087) on `AbstractListModel`. – trashgod Jul 18 '11 at 11:41
0

If you can get to that line, then it is likely that xTotal.preferred is null, or yTotal.preferred is null.

  • Obviously :p But that doesn't really help. – Bart van Heukelom Jul 15 '11 at 15:21
  • You asked how you could start debugging... this is the start :) What I would do is set a breakpoint there, and then inspect the variables. If either is null, then you need to track back to where those are initialized and figure out why. –  Jul 15 '11 at 15:37
0

From the amount of code I can see, I would guess that you are trying to do stuff with swing coponents without using an event dispatch thread..

This causes ugly exceptions like the one above. I had this issue in a project and I realized it too late to be able to fix it so I just put every thing in try cat and did not print the entire stack trace...

If this is indeed the problem you can possibly catch and ignore null pointer exceptions..

Best approach is to use the event dispatch threads

Osama Javed
  • 1,432
  • 1
  • 16
  • 21
  • P.s I believe your code might still be fucntional and the dimension might be created afterall as long as the exception does not stop the execution... I might be completely wrong as well :) – Osama Javed Jul 15 '11 at 15:18
0

Because the error happens "within" Swing (only java* classes on the stack trace), I'd take a very close look at my code to make sure all my Swing calls happen on the EDT.

Waldheinz
  • 10,399
  • 3
  • 31
  • 61