question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NPE while invoke toJson() or fromJson() for HashMap<String, String>

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:17

github_iconTop GitHub Comments

1reaction
Marcono1234commented, Aug 29, 2022

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.

0reactions
emanoellucasmlcommented, Aug 29, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found