Json file not having everything
So I have the class below, and my problem is, when I update it and add a new variable, if you launch it while you have already used it previously, your JSON file won't have all the variables needed, so they are just set to null, and that produces some problems later.
To fix this, the only idea I have is just a bunch of if statements, and while that's not really a big deal in the class example I just gave, I have other classes with many variables, and using if will make everything messy.
Anyone got any ideas? I have also thought about Gson having something for this case, but couldn't find anything.
11 Replies
⌛
This post has been reserved for your question.
Hey @MoonSouhayl! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Live long my post
I do not think they are set to
null
. Have you tried?💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
What do you want to happen when the variables aren't present?
you could create a method like this:
and then do
yourField = returnFirstIfNotNullOrSecond(yourField, defaultValue);
Note: that's for non-primitives
Aside from that, you could try just giving them a default value in the declarationI want them to have the values im giving them in the constructor instead of making them null
Yes
I have tried and they are set to null
Which values exactly are null?
aaah
nvm
i tested it the wrong way :b
my brain is not braining
We have all been there
i was adding values to a hashmap
and the file didnt have them, so it just made a hashmap without the values i added
and wehn trying to access them i was getting null pointer
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.