Q: We’re having an issue using PDFViewerBean when adding a button to the toolbar. The issue appears periodically.
The code:
JButton button = new JButton ("Some button"); PDFViewerBean.getSelectToolbar().add(button); button.addActionListener(someListener); |
The exception stack trace:
java.lang.NullPointerException
at java.awt.EventQueue$2.isDispatchThreadImpl(Unknown Source)
at sun.awt.SunToolkit.isDispatchThreadForAppContext(Unknown Source)
at javax.swing.JComponent.revalidate(Unknown Source)
at javax.swing.JComponent.setBorder(Unknown Source)
at javax.swing.plaf.basic.BasicToolBarUI.setBorderToRollover(Unknown Source)
at javax.swing.plaf.basic.BasicToolBarUI$Handler.componentAdded(Unknown Source)
at java.awt.Container.processContainerEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at javax.swing.JToolBar.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at company.package.PDFViewer.start(PDFViewer.java:127)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.start(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
A: This looks like a Swing issue. New buttons should be added within the Swing event thread. Try making the call to add a button to the toolbar within
SwingUtilities.invokeAndWait() |
.