NullPointerException at javax.swing.plaf.synth.SynthContext.getPainter

2.7k views Asked by At

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)
4

There are 4 answers

1
Grzegorz Oledzki On

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");

1
Axel On

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();
1
AlbertoLopez On

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.

2
Octavio On

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.