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.

Occasional NullPointerException when calling Gson.fromJson when converting to a TypedToken of type CopyOnWriteArraySet

See original GitHub issue

Gson version

2.8.9

Java / Android version

Multiple Android versions (23+)

Description

NullPointerException when calling Gson.fromJson when converting to a TypedToken of type CopyOnWriteArraySet. This crash is noticed on user devices but not reproducible locally while debugging.

Here is the code that is causing the null pointer.

        CopyOnWriteArraySet<String> result;
        Type type = new TypeToken<CopyOnWriteArraySet<String>>() {
        }.getType();
        Gson gson = new Gson();
        result = gson.fromJson(json, type);

Exception stack trace

Screenshot 2022-02-03 at 9 14 30 AM

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
kalyandechirajucommented, Aug 25, 2022

Apparently the issue has not resurfaced when we made the below two changes:

  1. Add complete proguard rules as per documentation
  2. Avoid casting json directly to CopyOnWriteArraySet. Instead casted in to an ArrayList and then pass it to constructor of CopyOnWriteArraySet.
0reactions
BoboBirdFlycommented, Aug 25, 2022

thanks! I’ll try it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Gson TypeToken NullpointerException - Stack Overflow
I am trying to parse a JSONArray using Gson. Whenever I am trying to initialize the object of class TypeToken. I see this...
Read more >
NullPointerException on a simple collection - Google Groups
attempt to de-serialize with this (on android client): Type listType = new TypeToken<List<Catagory>>() {}.getType(); retObj = gson.fromJson(result, listType);
Read more >
kalyandechiraju ( Kalyan Dechiraju ) - github record :)
Occasional NullPointerException when calling Gson.fromJson when converting to a TypedToken of type CopyOnWriteArraySet. Apparently the issue got fixed when ...
Read more >
Returning error code is always 0 - Wilfred/Difftastic - IssueHint
Occasional NullPointerException when calling Gson.fromJson when converting to a TypedToken of type CopyOnWriteArraySet, 6, 2022-02-03, 2022-08-04.
Read more >
JSON String to Java object using GSON
As the title said, here we want to convert a JSON string to a Java model by using GSON library. Basically we have...
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