Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException at java.awt.EventQueue.getCurrentEventImpl

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?

like image 502
tbeernot Avatar asked Apr 15 '15 12:04

tbeernot


2 Answers

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.

like image 67
tbeernot Avatar answered Oct 22 '22 14:10

tbeernot


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.

like image 23
robert engels Avatar answered Oct 22 '22 13:10

robert engels