Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

java.lang.IllegalStateException: Gson().fromJson(json,type) must not be null

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use the following function in my code :
 
when i use it at the begin of my app, the following error appear :

i don't know why,despite that json is string? type.
should you please tell me how can i correct it?
Capture-du-2019-07-31-12-41-24.png
[Thumbnail for Capture-du-2019-07-31-12-41-24.png]
 
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the complete exception and stack trace?
I'm just asking because that line only throws JsonSyntaxException, so I'm guessing that the IllegalState is the root cause?
 
Dave Tolls
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other thing to do is stick some debug logging in there to see what values everything has.
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the complete exception :

I use as you said the debug logging, the value is null
Capture-du-2019-07-31-14-09-29.png
[Thumbnail for Capture-du-2019-07-31-14-09-29.png]
 
Dave Tolls
Rancher
Posts: 4177
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, hang on.
Of course.

Your function is declared as returning a non-null ArrayList.
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i change my function as the following code:


And my problem is solved
 
Dave Tolls
Rancher
Posts: 4177
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the fix:
sharedPreferences.getString(key, emptyList)


Prior to that you were supplying a null json String to Gson, which resulted in a null value returned.
Now, you supply an empty list, which presumably results in a non-null value being returned.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!