Reputation: 39
Hashmap:
public HashMap<Perk, Boolean> perks;
public HashMap<Perk, Boolean> getPerks() {
return perks;
}
How I save (works fine, I'm using GSON by Google):
object.add("perks", builder.toJsonTree(player.perks));
Preview of save:
"perks": {
"DAMAGE_AMPLIFIER": true
}
How I am attempting to load but isn't working, error is below.
if (reader.has("perks")) {
jsonToMap(reader.get("perks").toString(), player);
}
jsonToMap method:
public static void jsonToMap(String t, Player player) throws JSONException {
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while(keys.hasNext()) {
String key = keys.next().toString();
boolean value = jObject.getBoolean(key);
player.perks.put(Perk.valueOf(key), value);
}
}
Error:
java.lang.NullPointerException
at com.callisto.world.entity.impl.player.PlayerLoading.jsonToMap(PlayerLoading.java:730)
at com.callisto.world.entity.impl.player.PlayerLoading.getResult(PlayerLoading.java:684)
Upvotes: 1
Views: 119
Reputation: 7071
If I have to guess perks
is null and that's why when you do put
you get a null pointer. Change it to:
public HashMap<Perk, Boolean> perks=new HashMap<>(); //We init the hashmap here so we can putt stuff after that
public HashMap<Perk, Boolean> getPerks() {
return perks;
}
Also you might even change it to :
Map<Perk, Boolean> perks=new HashMap<>();
and use the interface and not the implementation in your methods
Upvotes: 1