编辑:空指针是由于形成错误的json。
您好,
我正在尝试将json数据反序列化为Restaurant对象的ArrayList,如下所示(由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)
但这会给出帖子末尾显示的错误。
似乎listType中有一些空指针......这是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)
我的代码出了什么问题?我怎么能这样做?
由于
月
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