2

A Java Swing program I work on keeps getting the exception below. It happens at random times and is far from reproducible. It does not seem to usually cause any problem other than on time action events are not triggered but usually even after this exception things work fine. There seems to be no consistency to its happening. Any one have any advice? I should mention that we are using the nimbus LAF.

java.lang.NullPointerException
at javax.swing.plaf.synth.SynthContext.getPainter(SynthContext.java:181)
at javax.swing.plaf.synth.SynthPanelUI.update(SynthPanelUI.java:95)
at javax.swing.JComponent.paintComponent(JComponent.java:752)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at org.jdesktop.jxlayer.JXLayer.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:785)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
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)
kleopatra
  • 51,061
  • 28
  • 99
  • 211
startoftext
  • 3,846
  • 7
  • 40
  • 49

4 Answers4

4

I get the same error sometimes when invoking:

 JComponent.updateUI() 

using Nimbus Look & Feel. In my case, such invocation was not necessary so I removed the line.

Octavio
  • 41
  • 2
  • note that using updateUI in application code is _wrong_ nearly always, so good that you removed it :-) – kleopatra Nov 21 '12 at 10:15
  • I think that basically is the cause. Some developers on my team often invoked this to get swing to refresh its UI and at times they also invoked it from a thread that was not part of the event dispatch thread. Generally speaking its not a good idea to touch swing objects from within a non EDT thread which this program did allot of. I no longer work for that company so its not my problem :-) – startoftext Nov 30 '12 at 23:58
3

This is quite a popular bug if you searched in Google.

One of the sites suggests this:

replacing the line

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

with:

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
  • Not using Metal look and feel. Using a modified version of nimbus. Basically we instantiate a nimbus LAF and then modify its properties before calling setLookAndFeel(...). I tied a variation of what you suggest only with nimbus and we still have the same exception at very random times. – startoftext Feb 27 '11 at 03:42
0

I had the same problem and was able to fix it, I have two suggestions if you are using SwingWorkers.

1) In your worker's doInBackground method try to catch any Runtime or uncaught exceptions, so you can verify that your method is not exiting before you think it is.

2) Verify that you are not updating any Swing component outside the worker's property change events. Remember that all swing components should be updated only in the event thread not in the worker's thread.

Hope this helps.

AlbertoLopez
  • 101
  • 3
  • It was 7 years ago but yes I think it was something related to this. In the Swing app this was happening in the other developers were spawning threads all over the place and Swing is not really intended to be accessed from multiple threads like that. – startoftext Jan 25 '19 at 17:25
0

I got this error after trying to repaint a swing component with the following method:

SwingUtilities.updateComponentTreeUI(COMPONENT); 

where COMPONENT is the swing component that needed to be repainted.

I finally resolved this problem replacing the above code with this

COMPONENT.validate();
COMPONENT.repaint();
Mia Clarke
  • 8,134
  • 3
  • 49
  • 62
Axel
  • 1
  • Does it mean that validate() and repaint() are enough to refresh the UI. Don't we need a COMPONENT.updateUI() afterwards anymore? – riroo May 25 '16 at 11:39