tags:

views:

115

answers:

2

EDIT: the null pointer was due to a badly formed json.

Hi,

I'm trying to deserialize json data to an ArrayList of Restaurant object as follows (inpsired by what I found in https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples):

Type listType = new TypeToken<ArrayList<Restaurant>>() {}.getType();
ArrayList<Restaurant> objList = gson.fromJson( r, listType );         //(line 141)

but this gives the error shown at the end of the post.

It seems that there is some null pointer in listType... Here's a watch of listType:

"listType"= ImplForType  (id=830061042288)  
    args= ListOfTypes  (id=830061040752)    
        list= null  
        resolvedTypes= Type[1]  (id=830061042800)   
    loader= PathClassLoader  (id=830060323064)  
    ownerType0= null    
    ownerTypeRes= null  
    rawType= Class (java.util.ArrayList) (id=830002592616)  
    rawTypeName= "java.util.ArrayList" (id=830061042328)    

What's wrong with my code? How can I do that?

Thanks

Jul

07-06 17:38:48.825: DEBUG/dalvikvm(1337):   JDWP invocation returning with exceptObj=0x4364a4a0
07-06 17:39:00.525: DEBUG/dalvikvm(1337):   JDWP invocation returning with exceptObj=0x4364b1e8
07-06 17:39:15.879: WARN/dalvikvm(1337): threadid=17: thread exiting with uncaught exception (group=0x4000fe70)
07-06 17:39:15.885: ERROR/AndroidRuntime(1337): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-06 17:39:16.135: DEBUG/dalvikvm(1337): GC freed 3322 objects / 261128 bytes in 114ms
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.util.concurrent.FutureTask.run(FutureTask.java:122)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.lang.Thread.run(Thread.java:1058)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): Caused by: java.lang.NullPointerException
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.lang.StringBuilder.append(StringBuilder.java:209)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:65)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at com.google.gson.Gson.fromJson(Gson.java:379)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at org.digitalfarm.atable.Atable$GetRestaurantData.doInBackground(Atable.java:141)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at org.digitalfarm.atable.Atable$GetRestaurantData.doInBackground(Atable.java:1)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337):     ... 4 more
A: 

Is Restaurant Serializeable? If it isn't that might be your problem.

Christian
GSON takes care of that.https://sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples
jul
That link contains the passage: "However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization."
Christian
This is for nested class. What make me think I can do what I want is that I first tried with a class RestaurantList, with private List<Restaurant> restaurants = new ArrayList<Restaurant>(); as a member, and RestaurantList restaurantList = gson.fromJson(r, RestaurantList.class); worked.
jul
A: 

this is a known bug that we've fixed in gingerbread; see http://code.google.com/p/android/issues/detail?id=6636

Elliott Hughes