Bug: Problem invoking native javascript methods with Array parameters

20 views
Skip to first unread message

aglaforge

unread,
Jun 29, 2006, 1:16:56 AM6/29/06
to Google Web Toolkit
I've found this problems twice in dealing with the google maps GWT
project... however given that I've seen it in multiple places I believe
it's more of a general problem.

public native void expOpenInfoWindowTabsHtml(GMarker self)/*-{
self.openInfoWindowTabsHtml([new $wnd.GInfoWindowTab("test","tab")]);
}-*/;

If you try and invoke a method on a JavaScriptObject which contains an
array as a parameter you'll get a null exception. In this case, the
above method takes an array of GInfoWindowTab objects.

All the other methods without array parameters work without any
problem, for example executes without problem:

public native void openInfoWindow(GMarker self, Element content)/*-{
self.openInfoWindow(content);
}-*/;

Thus, the issue appears to be stricly limited to when an array is
present in the parameters (even when the array is created in the JSNI
enviroment as in the previous example).

If the GWT could look into this issue, I'd greatly appreciate it.

Kind Regards,

Anthony L.

aglaforge

unread,
Jun 29, 2006, 1:56:22 PM6/29/06
to Google Web Toolkit
For clarity, the assoicated stack trace is as follows....Since I don't
really have visibility into the invokeNative method I can't really say
for sure what's going wrong, however it's clear it is a caused by a
JavaScript array.

[ERROR] Unable to load module entry point class
com.mycompany.client.MyApplication
java.lang.NullPointerException: null
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.invokeNative(ModuleSpaceIE6.java:377)
at
com.google.gwt.dev.shell.ie.ModuleSpaceIE6.invokeNativeVoid(ModuleSpaceIE6.java:283)
at
com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:127)
at
com.mapitz.gwt.googleMaps.client.GMarkerImpl.openInfoWindowTabsHtml(GMarkerImpl.java:78)
at
com.mapitz.gwt.googleMaps.client.GMarker.openInfoWindowTabsHtml(GMarker.java:98)
at
com.mycompany.client.MyApplication.onModuleLoad(MyApplication.java:238)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:64)
at
com.google.gwt.dev.shell.BrowserWidget.attachModuleSpace(BrowserWidget.java:324)
at
com.google.gwt.dev.shell.ie.BrowserWidgetIE6.access$100(BrowserWidgetIE6.java:19)
at
com.google.gwt.dev.shell.ie.BrowserWidgetIE6$External.gwtOnLoad(BrowserWidgetIE6.java:45)

BigPine

unread,
Jun 29, 2006, 5:55:08 PM6/29/06
to Google Web Toolkit
I've encountered this issue as well ...

John Deck

bruciadmin

unread,
Jul 26, 2006, 3:11:06 AM7/26/06
to Google Web Toolkit
I've been using the Google Maps GWT project too and have encountered
issues with passing Javascript arrays back into GWT Java (I've lodged a
bug on the sourceforge site). When I've done variable inspection on a
GWT Javascript array, it has many more member variables - whereas the
"true" array is just that and doesn't have any of the extra fields.

I think some clarity is needed regarding moving array objects from
Javascript to GWT Java via JSNI (and vice versa). I have a feeling
there are workarounds to these issues but don't know one personally.

Any takers.... ?

Scott Blum

unread,
Jul 27, 2006, 1:30:15 PM7/27/06
to Google Web Toolkit
Java arrays and JavaScript arrays are very different beasts, and they
cannot be marshalled back and forth. Currently, the only way I know of
to work around this is to use JSNI accessors to copy values one at a
time.

Something like this:

static native JavaScriptObject createJavaScriptArray() /*-{
return [];
}-*/;

static native void setValue(JavaScriptObject array, int index, String
value) /*-{
array[index] = value;
}-*/;

static native String getValue(JavaScriptObject array, int index) /*-{
var result = array[index];
return (result == null) ? null : result;
}-*/;

static native int getLength(JavaScriptObject array) /*-{
return array.length;
}-*/;

You would need differently-typed accessor methods for all the major JS
types- String, double, boolean, JavaScriptObject, or even Object if you
needed to store Java objects in a JS array.

http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.JavaScriptNativeInterface.Marshaling.html
is a good reference.

Scott

Reply all
Reply to author
Forward
0 new messages