Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppContext is null from rmi thread with Java 7 Update 25

We recently updated to Java 7 Update 25 from Update 21 and are now experiencing a null pointer exception when SwingUtilities.isEventDispatchThread() is called from an rmi thread becuase AppContext.getAppContext() is returning null.

java.lang.NullPointerException at sun.awt.SunToolkit.getSystemEventQueueImplPP(Unknown Source) at sun.awt.SunToolkit.getSystemEventQueueImplPP(Unknown Source) at sun.awt.SunToolkit.getSystemEventQueueImpl(Unknown Source) at java.awt.Toolkit.getEventQueue(Unknown Source) at java.awt.EventQueue.isDispatchThread(Unknown Source) at javax.swing.SwingUtilities.isEventDispatchThread(Unknown Source) at ... ... at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Unknown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

This error is only present from web start, when we run our application through an IDE, it is fine.

Has anyone else ran into this? Any idea of what was changed in the latest update regarding AppContext?

It seems others are having somewhat related issues with AppContext after the update: https://forums.oracle.com/message/11077767#11077767

like image 339
RobinFago Avatar asked Jun 20 '13 20:06

RobinFago


1 Answers

I had the same problem with Java3D running as Java Web Start. I have found another solution. You have to prepare you own InvokeLaterProcessor with queue of runnables. It must extend Thread and pick ups runnables and process them in run method:

public class InvokeLaterProcessor extends Thread {

  private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<Runnable>(1);

  public InvokeLaterProcessor(String name) {
    super(name);
  }

  public void invokeLater(Runnable runnable) {
    try {
      queue.put(runnable);
    } catch (InterruptedException ex) {
      log.warn("invokeLater interrupted");
    }
  }

  public void run() {
    Runnable runnable=null;
    do {
      try {
        runnable = queue.take();
        SwingUtilities.invokeLater(runnable);
      } catch (InterruptedException ex) {
        runnable=null;
      }
    } while(runnable!=null);
  }
}

Than all you have to do is create it in static of some class which is created in main thread:

static {
  invokeLaterProcessor=new InvokeLaterProcessor("MyInvokeLater");
  invokeLaterProcessor.start();
}

and process runnables by this code:

invokeLaterProcessor.invokeLater(runnable);

You don't need proprietary

sun.awt.SunToolkit.invokeLaterOnAppContext(evtContext, rn)
like image 110
user2641449 Avatar answered Nov 20 '22 08:11

user2641449