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
JavaBot
JavaBot10mo ago
This post has been reserved for your question.
Hey @MoonSouhayl! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 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.
MoonSouhayl
MoonSouhaylOP10mo ago
Live long my post
Peter Rader
Peter Rader10mo ago
I do not think they are set to null. Have you tried?
JavaBot
JavaBot10mo ago
💤 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.
dan1st
dan1st10mo ago
What do you want to happen when the variables aren't present? you could create a method like this:
private <T> T returnFirstIfNotNullOrSecond(T first, T ifNull){
return first == null ? second : first;
}
private <T> T returnFirstIfNotNullOrSecond(T first, T ifNull){
return first == null ? second : first;
}
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 declaration
MoonSouhayl
MoonSouhaylOP10mo ago
I 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
dan1st
dan1st10mo ago
Which values exactly are null?
MoonSouhayl
MoonSouhaylOP10mo ago
aaah nvm i tested it the wrong way :b my brain is not braining
dan1st
dan1st10mo ago
We have all been there
MoonSouhayl
MoonSouhaylOP10mo ago
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
JavaBot
JavaBot10mo ago
💤 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.

Did you find this page helpful?