Reputation: 9
I'm developing a Swing application that involves lots of classes and most times it works, while some times it crashes with a NullPointerException
. When viewed in a console, the stacktrace does not include any of my classes. Because of this, I can't figure out where the origin of the problem is.
Here is the stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.GroupLayout$ComponentInfo.setBounds(GroupLayout.java:3657)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:928)
at java.awt.Container.layout(Container.java:1508)
at java.awt.Container.doLayout(Container.java:1497)
at java.awt.Container.validateTree(Container.java:1693)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validate(Container.java:1628)
at javax.swing.RepaintManager$2.run(RepaintManager.java:691)
at javax.swing.RepaintManager$2.run(RepaintManager.java:689)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:688)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1679)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Based on the stacktrace I just want to know what could be the problem. I need ideas that will point me to the right direction. Thanks.
Upvotes: 0
Views: 195
Reputation: 719386
The code for the javax.swing.GroupLayout.ComponentInfo.setBounds
method is as follows:
public void setBounds(Insets insets, int parentWidth, boolean ltr) {
int x = horizontalSpring.getOrigin();
int w = horizontalSpring.getSize();
int y = verticalSpring.getOrigin();
int h = verticalSpring.getSize();
if (!ltr) {
x = parentWidth - x - w;
}
component.setBounds(x + insets.left, y + insets.top, w, h);
}
If a NPE is thrown by that method, then it follows that one (or more) of insets
, horizontalSpring
, verticalSpring
or component
is null
. (If we knew precisely which line the exception was thrown on, it would be possible to narrow this down to one or two possibilities.)
Going further than this is difficult without seeing your code, but I suspect that there is an error in the way that you have constructed a GroupLayout
.
@biziclop's theory about updates is also plausible. Swing's component data structures are NOT thread-safe, and should NOT be updated by any thread other than the swing event thread.
Upvotes: 3
Reputation: 49804
It's a tough job when that happens but there are two places you can start at:
GroupLayout
, This reveals that the line in question is probably int x = horizontalSpring.getOrigin();
(this may depend on your JDK version). This isn't terribly helpful but it's a start. If you look at where horizontalSpring
is set, one thing immediately stands out: it's set to null
in the dispose()
method. So maybe your component is already disposed and that's why you're getting the error.A further clue is that you say it works most of the time. In the vast majority of cases this means some kind of threading/synchronization issue, maybe you're updating the component hierarchy from a thread that isn't the Event Dispatcher
.
Upvotes: 3