I've migrated a Swing application to Java 8 and recently we see the following NPE exception.
java.lang.NullPointerException
at java.awt.EventQueue.getCurrentEventImpl(EventQueue.java:848)
at java.awt.EventQueue.getCurrentEvent(EventQueue.java:842)
at java.awt.Component.requestFocusHelper(Component.java:7628)
at java.awt.Component.requestFocusHelper(Component.java:7620)
at java.awt.Component.requestFocus(Component.java:7495)
at javax.swing.JComponent.requestFocus(JComponent.java:1504)
at javax.swing.plaf.basic.BasicPopupMenuUI$MenuKeyboardHelper.stateChanged(BasicPopupMenuUI.java:1173)
at javax.swing.MenuSelectionManager.fireStateChanged(MenuSelectionManager.java:202)
at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:129)
at javax.swing.JPopupMenu.setVisible(JPopupMenu.java:784)
at javax.swing.JPopupMenu.show(JPopupMenu.java:965)
at org.tbee.swing.StandardComponentPopupMenu.showJTableMenu(StandardComponentPopupMenu.java:555)
at org.tbee.swing.StandardComponentPopupMenu$2.run(StandardComponentPopupMenu.java:175)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
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)
Migrating back to J7 is a problem, because lambda's are being used, so I cannot easily confirm that this really is a J8 issue. But the code in question has been running for several years now, surviving Java 5, 6 and 7. So chances are it is J8 specific.
The reason for the exception is that currentEvent in EventQueue is not set. This is done by its setCurrentEventAndMostRecentTimeImpl(AWTEvent e). However, if I trace back in the stack, I see that in EventQueue:756 (Java 1.8.0u45) the dispatch is done after exactly that method is called.
if (event instanceof ActiveEvent) {
// This could become the sole method of dispatching in time.
setCurrentEventAndMostRecentTimeImpl(event);
((ActiveEvent)event).dispatch();
I'm in the dark why that variable is null. Debugging is close to impossible, because of all the events being handled by that piece of code.
Is anyone aware of changes in Swing's event handling in J8?
In the end, the opening of the popup menu had to be rescheduled on the EDT, even though the code opening it is running on the EDT.
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jpopupMenu.show(c, x, y);
}
});
Only when running on J8.
Also, ensure that if you are installing an alternate event queue that you are doing it on it's own event, like:
Swingutilities.invokeLater(){
... run() {
..do event queue push();
}
}
The problem is that the event queue push is not migrating the current event, only pending events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With