NullPointerException on a simple collection

996 views
Skip to first unread message

Benjamin

unread,
Jul 19, 2010, 4:31:12 PM7/19/10
to google-gson
This has been killing me all day - thanks in advance to anyone who can
take a look. Maybe i just need a fresh set of eyes. i'm just trying to
serialize and de-serialize a list of a very simple object that has one
property

GSON 1.4

This JSON String:

[{"name":"Nimbits Stats"},{"name":"TestPoints"},{"name":"My Health"},
{"name":"System"},{"name":"Aquarium"},{"name":"Nimbits_Unsorted"},
{"name":"House"},{"name":"test"},{"name":"BEN-UBSV01"}]

Is generated on server from an arraylist of objects of this class:

public class Catagory
{
private String name;
//..getters and setters
}

thusly:

Type listType = new TypeToken<List<Catagory>>() {}.getType();
retStr = gson.toJson(myarraylist, listType);

attempt to de-serialize with this (on android client):

Type listType = new TypeToken<List<Catagory>>() {}.getType();
retObj = gson.fromJson(result, listType);

results in:

07-19 20:16:34.823: ERROR/AndroidRuntime(285): FATAL EXCEPTION: main
07-19 20:16:34.823: ERROR/AndroidRuntime(285):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.nimbits.android/
com.nimbits.android.CatagoryActivity}: java.lang.NullPointerException
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2663)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2679)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.os.Looper.loop(Looper.java:123)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.ActivityThread.main(ActivityThread.java:4627)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
java.lang.reflect.Method.invokeNative(Native Method)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
java.lang.reflect.Method.invoke(Method.java:521)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
dalvik.system.NativeStart.main(Native Method)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): Caused by:
java.lang.NullPointerException
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:
47)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:
83)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
java.lang.StringBuilder.append(StringBuilder.java:203)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:
56)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:
88)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:
76)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:98)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:
64)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:
49)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.Gson.fromJson(Gson.java:495)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.Gson.fromJson(Gson.java:444)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.google.gson.Gson.fromJson(Gson.java:396)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.nimbits.google.DataClient.getJSONCatagories(DataClient.java:175)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.nimbits.android.CatagoryActivity.createDB(CatagoryActivity.java:
118)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
com.nimbits.android.CatagoryActivity.onCreate(CatagoryActivity.java:
246)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1047)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2627)
07-19 20:16:34.823: ERROR/AndroidRuntime(285): ... 11 more



inde...@gmail.com

unread,
Jul 19, 2010, 5:06:33 PM7/19/10
to google-gson
The JSON looks fine to me.

Are you registering any custom deserializers?
I looked at line 88 of
http://code.google.com/p/google-gson/source/browse/trunk/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java
and it can throw an NPE if you registered a null as the deserializer
for a type. However, GsonBuilder doesn't allow you to register nulls
in this way, so I am confused.

Inder

Benjamin

unread,
Jul 19, 2010, 5:47:04 PM7/19/10
to google-gson
Thanks Inder,

I just got it working

1. On the client I was using a nested class, so it had to be marked as
static.
2. the class requires a no-args constructor - adding that helped.

public static class Catagory
{
Catagory() {
// no-args constructor
}

private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}




On Jul 19, 5:06 pm, "inder...@gmail.com" <inder...@gmail.com> wrote:
> The JSON looks fine to me.
>
> Are you registering any custom deserializers?
> I looked at line 88 ofhttp://code.google.com/p/google-gson/source/browse/trunk/gson/src/mai...
Reply all
Reply to author
Forward
0 new messages