使用GWT和elemental,我遇到了一个null实例的问题。这是代码:
import elemental.events.Event;
import elemental.events.EventListener;
import elemental.html.Window;
import elemental.html.Worker;
public void go()
{
Window window=elemental.client.Browser.getWindow();
Worker worker=window.newWorker("task.js");
EventListener eventListener=new EventListener()
{
@Override
public void handleEvent(Event event)
{
}
}
System.out.println("worker : "+worker+" eventListener : "+eventListener+" window : "+window);
worker.setOnmessage(eventListener);
}
显示为:
worker : [object Worker] eventListener : mainpackage.client.MainClass$1@565f81ea window : [object Window]
所以worker不是null,但我在setOnmessage中有错误:
com.google.gwt.core.client.JavaScriptException: (String) : Invoking an instance method on a null instance
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.gwt.dev.shell.ModuleSpace.createJavaScriptException(ModuleSpace.java:80)
at com.google.gwt.dev.shell.ModuleSpace.createJavaScriptException(ModuleSpace.java:64)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:60)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:304)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
at elemental.js.html.JsWorker$.setOnmessage$(JsWorker.java)
at com.google.gwt.core.client.JavaScriptObject$.elemental_html_Worker_setOnmessage(JavaScriptObject.java)
at mainpackage.client.MainClass.go(MainClass.java:41)
at mainpackage.client.Testthread.onModuleLoad(Testthread.java:30)
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 com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:411)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
为什么有空实例?我该如何解决?提前谢谢。
答案 0 :(得分:1)
com.google.gwt.core.client.JavaScriptException: (String) : Invoking an instance method on a null instance
此行有答案。
当您尝试调用
时 worker.setOnmessage(eventListener);
Worker worker=window.newWorker("task.js");
worker
对象为NULL。
因此,请检查您的newWorker
方法是否实际返回Worker
个对象。