我们最近从更新21更新到Java 7更新25,现在由于从AppContext.getAppContext()返回null而从rmi线程调用SwingUtilities.isEventDispatchThread()时,现在遇到了空指针异常。



仅当从网络启动时才出现此错误,当我们通过IDE运行应用程序时,就可以了。

还有其他人碰到这个吗?是否知道有关AppContext的最新更新中发生了什么更改?

似乎其他人在更新后与AppContext都有一些相关的问题:
https://forums.oracle.com/message/11077767#11077767

最佳答案

我在与Java Web Start一起运行Java3D时遇到了同样的问题。我找到了另一种解决方案。您必须准备自己的InvokeLaterProcessor和可运行队列。它必须扩展Thread并拾取可运行对象,并在run方法中对其进行处理:

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);
  }
}

比您要做的就是在某个在主线程中创建的类的static中创建它:
static {
  invokeLaterProcessor=new InvokeLaterProcessor("MyInvokeLater");
  invokeLaterProcessor.start();
}

并通过以下代码处理可运行对象:
invokeLaterProcessor.invokeLater(runnable);

您不需要专有
sun.awt.SunToolkit.invokeLaterOnAppContext(evtContext, rn)

09-28 14:27