gson: NPE while invoke toJson() or fromJson() for HashMap
the exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
libcore.reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47)
java.util.HashMap.hash(HashMap.java:338)
java.util.HashMap.containsKey(HashMap.java:595)
java.util.HashSet.contains(HashSet.java:203)
com.google.gson.internal.$Gson$Types.resolve(.java:346)
com.google.gson.internal.$Gson$Types.resolve(.java:381)
com.google.gson.internal.$Gson$Types.resolve(.java:337)
com.google.gson.internal.$Gson$Types.getSupertype(.java:283)
com.google.gson.internal.$Gson$Types.getCollectionElementType(.java:302)
com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:52)
com.google.gson.Gson.getAdapter(Gson.java:458)
com.google.gson.Gson.toJson(Gson.java:696)
com.google.gson.Gson.toJson(Gson.java:683)
com.google.gson.Gson.toJson(Gson.java:638)
com.google.gson.Gson.toJson(Gson.java:618)
my code:
new Gson().fromJson(json, new TypeToken<HashMap<String, String>>() {}.getType());
environment
Gson version 2.8.5 The probability of this problem happens substantial increases,when gson version upgrades from 2.8.2 to 2.8.5 !
Same problem with https://github.com/google/gson/issues/1298 , but not resolved.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 7
- Comments: 19
https://issuetracker.google.com/issues/220725541
I just mentioned using the latest Gson version to avoid having to debug issues in older versions, which might already be fixed in the latest version. Though this appears to be caused by ProGuard (or R8) so I assume adjusting the ProGuard rules without upgrading Gson might work as well.