0

I'm working with swing in Java and in my program I have to make some calculations. These calculations take a lot of time so I decided to create a thread for these calculations and from the thread, change the gui.

sequence diagram

The problem is that when the thread completes the calculations, add information in a tab of a jtabbedPane but when I try to change the title of this tab it use the method setTitleAt and call updateUI() and the program throw the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthTabbedPaneUI.getFontMetrics(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateTabRects(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateLayoutInfo(Unknown Source)
at javax.swing.plaf.synth.SynthTabbedPaneUI$2.calculateLayoutInfo(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at javax.swing.RepaintManager$2.run(Unknown Source)
at javax.swing.RepaintManager$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Is it posible that this error appears because the call it's maked by the thread?

Magnilex
  • 11,584
  • 9
  • 62
  • 84
lucasasecas
  • 944
  • 2
  • 9
  • 18
  • your main problem is that you have a NPE, null pointer exception, not that you're not threading correctly (although this may also be a problem). You need to debug this by looking at the line that throws the exception. – Hovercraft Full Of Eels Mar 25 '15 at 19:00
  • You should never call `updateUI()`, not unless you're changing the look and feel. You need to show pertinent code and to isolate the error. – Hovercraft Full Of Eels Mar 25 '15 at 19:01

1 Answers1

1

Regardless of the other problems you're having, you should not directly update the UI from something that is not the AWT event thread. If you have an asynchronous process (like your calculation thread) that needs to modify the UI, you need to queue that operation for the event thread to execute it. Use SwingUtilites.invokeLater() and pass it a Runnable that will make the necessary UI changes. I haven't tried this exact scenario, but I bet you can create the panel in the worker thread and use the Runnable to attach it to the tabbed pane.

Jose Ferrer
  • 1,188
  • 1
  • 7
  • 9