Exception in GWT Elemental Simple Example

725 views Asked by At

I am attempting to run the GWT Elemental example (from the GWT 2.5.1 repository) and it is giving me an exception at runtime. I had to make a couple of changes to the code in order to get this to run, which I am guessing is a result of changes to Elemental that are not reflected in the example code. Here is the code, with my changes noted:

@Override
public void onModuleLoad() {
  final ButtonElement btn = getDocument().createButtonElement();
  btn.setInnerHTML("w00t?");
  btn.getStyle().setColor("red");
  getDocument().getBody().appendChild(btn);
  final DivElement div = getDocument().createDivElement();
  getDocument().getBody().appendChild(div);
  EventListener listener = new EventListener() {
    @Override
    public void handleEvent(Event evt) {
      final XMLHttpRequest xhr = getWindow().newXMLHttpRequest();
      xhr.setOnload(new EventListener() {
        @Override
        public void handleEvent(Event evt2) {
          div.setInnerHTML(xhr.getResponseText());
        }
      });
      xhr.open("GET", "/snippet.html");
      xhr.send();
      getWindow().setTimeout(new TimeoutHandler() { // *** changed from "new Window.TimerCallback()"
        @Override
        public void onTimeoutHandler() { // *** changed from "public void fire()"
          getWindow().alert("timeout fired");
        }
      }, 1000);
      btn.removeEventListener(Event.CLICK, this, false);
    }
  };
  btn.addEventListener(Event.CLICK, listener, false);
}

The original code is here: ElementalExample.java.

The exception is occurring on the line: btn.addEventListener(Event.CLICK, listener, false);, and appears to be caused by a null object in one of the JavaScript libraries. If I comment out the btn.addEventListener() call then the page is shown in the browser, with the button (although it is non-functional, of course, with no event listener). Has anyone else tried to run this example? If so, do you have any pointers for running in the the latest release of GWT?

Here's the stack trace:

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:571)
 at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
 at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
 at elemental.js.dom.JsElementalMixinBase$.addEventListener$(JsElementalMixinBase.java)
 at com.google.gwt.core.client.JavaScriptObject$.elemental_html_ButtonElement_addEventListener(JavaScriptObject.java)
 at org.greatlogic.elementalfile.client.ElementalFileEntryPoint.onModuleLoad(ElementalFileEntryPoint.java:48)
 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:406)
 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)

(Note that I have pasted the code into my own project, and hence the org.greatlogic.elementalfile.client.ElementalFileEntryPoint classname).

0

There are 0 answers