julman99 / gson-fire

A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getters) serialization, pre/post processors and many others. Check out the documentation to learn how to use it!
http://gsonfire.io
Other
226 stars 36 forks source link

Null pointer exception when serializing object with null field AND while using hooks and type selectors #25

Closed knezmilos13 closed 8 years ago

knezmilos13 commented 8 years ago

I've mostly been using GSON and gson-fire for deserialization, and I've had no problems with that. However, I've been having a following problem when trying to serialize data. I've tried using the same GSON instance I use for deseriailzation, and this istance has hooks and a type selector enabled. When trying to serialize object that has a null value in one of his variables (both the object and the null attribute being of types covered by my custom type selector), I get a null pointer exception at HooksInvoker class, line 47. When I disable hooks, I get an exception at TypeSelectorTypeAdapterFactory, line 48. In both cases value/object variables are null when processing reaches the null object which is a part of the object being seriailzed.

I've momentarily solved this by using a different GSON instance for serialization, which doesn't use hooks nor type selection but that might not work for me in the future, and this seems to be a bug of some kind. At the very least, I think that hooks should totaly be ignored if object is null. Probably something similar should be done with the type selection.

Your library was otherwise very helpful to me, and I'm hoping you'll take a look at this. Thanks!

julman99 commented 8 years ago

Thanks for this report, I will look into it during this week

julman99 commented 8 years ago

Sorry for the delay on my response. Please try version 1.5.1 and let me know if it fixes your problem.

Thanks!

knezmilos13 commented 8 years ago

Hey, I've changed my code due to some other requirements so this thing stopped being an issue to me, and I forgot to pull your new version. Until today, that is, when I got a similar exception to these previous ones, but while deserializing this time:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Not a JSON Object: null com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:221) com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:285) io.gsonfire.gson.TypeSelectorTypeAdapterFactory$TypeSelectorTypeAdapter.read(TypeSelectorTypeAdapterFactory.java:60) ...

So I've pulled in your new version just now, and it actually works even in this case - I guess your fix solved a wider array of null-caused problems! Thanks :D

julman99 commented 8 years ago

nice! I did fix other potential null pointer exceptions based on your initial report

thanks for helping out!