NPE while invoke toJson() or fromJson() for HashMap<String, String>
See original GitHub issuethe 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:17
Top Results From Across the Web
NullPointerException : JSON Parsing in JAVA using GSON
In your current code , you are trying to invoke get() on the null object reference, which throws NullPointerException , as you are...
Read more >NullPointerException on a simple collection - Google Groups
This JSON String: ... fromJson(result, listType); results in: ... invokeCustomDeserializer(JsonDeserializationVisitor.java:
Read more >Exception in thread "main" java.lang.NullPointerException
fromJson ( str, new TypeToken<HashMap<String, Object>>() {}.getType() ); return map ... toString()" because the return value of "java.util.
Read more >Jackson JSON Java Parser API Example Tutorial - DigitalOcean
Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson...
Read more >getting error as java.lang.NullPointerException............
i created and JSON webservice and tested it was working flawlessly. In android when i give its URL and execute the program means...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Hello, does anyone know if doing what @Marcono1234 suggested here the problem is solved? I am going through the same problem but updating the library version would affect many people, so I wanted to be sure before doing it.