views:

219

answers:

1

Hi there,

I'm trying to run the following line:

Directions.loadFromWaypoints((Waypoint[])waypoints.toArray(), opts);

But I'm getting:

23:41:44.595 [ERROR] [carathome] Uncaught exception escaped
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.google.gwt.maps.client.geocode.Waypoint;
    at com.presasystems.gwt.carathome.client.widgets.MostrarLinhasPanel$1$1.onSuccess(MostrarLinhasPanel.java:72)
    at com.presasystems.gwt.carathome.client.widgets.MostrarLinhasPanel$1$1.onSuccess(MostrarLinhasPanel.java:1)
    at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:216)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:393)
    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.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannel.reactToMessagesWhileWaitingForReturn(BrowserChannel.java:1713)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:165)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)
    at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1668)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
    at java.lang.Thread.run(Unknown Source)

Why? Shouldn't this cast work? How can I do this in an elegant fashion? Thanks in advance

+1  A: 

Try

Directions.loadFromWaypoints((Waypoint[])(waypoints.toArray()), opts);

Alternatively

Waypoint[] array = new Waypoint[0];
array = waypoints.toArray(array);
Directions.loadFromWaypoints(array, opts);

Or more simply

Waypoint[] array = waypoints.toArray(new Waypoint[0]);
Directions.loadFromWaypoints(array, opts);

See also List#toArray(T[] a).

Addendum: Initially, I thought your cast was a precedence problem. The generic parameter T in List#toArray(T[] a) eliminates the need for an explicit cast by ensuring that the "runtime type of the returned array is that of the specified array." In effect, it "acts as bridge between array-based and collection-based APIs."

trashgod
It didn't work, sorry
Thiago
Can you post a link to the API?
trashgod
http://gwt-google-apis.googlecode.com/svn/javadoc/maps/1.0/index.html
Thiago
What's the declaration of `waypoints`, and what new error are you getting?
trashgod
ArrayList<Waypoint> waypoints = new ArrayList<Waypoint>();Error:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.google.gwt.maps.client.geocode.Waypoint;
Thiago
This looks like the same exception; you might try the other `toArray()` method of `List`.
trashgod
It is the same. I don't see how List would solve the problem, because it is an interface. It would actually do the exactly same thing. But your array proposal is right. I was just wondering what's wrong with the code I posted.
Thiago
I would advocate coding to the interface. http://www.javaworld.com/javaworld/jw-08-1999/jw-08-interfaces.html
trashgod
I agree, but that's not what I asked.
Thiago
Yes, I expanded on why your original code fails above.
trashgod