Solved storing custom items to config

Discussion in 'Plugin Development' started by xelatercero, Jan 23, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    xelatercero

    So I have a problem when storing custom items into config, basically when the server shuts down all i save a HashMap that contains the player UUID as the key, and the object "MailStorage".

    The MailStorage contains a list of HashMaps with a player UUID as the key and a List<ItemStack> as the value,
    the thing is all works just fine if i store minecraft items and store them to config, the thing is if i store a custom item into the list of HashMaps all works well until i store them into config wich throws a:
    Code:
    Caused by: java.lang.NullPointerException: null key in entry: null=0
    With any other minecraft item that doesnt happen.

    Code:java
    1. public void serializeStorage() {
    2.  
    3. for(Entry<UUID, MailStorage> entry : mailStorage.entrySet()) {
    4. if(entry != null) {
    5. UUID playerUUID = entry.getKey();
    6. MailStorage storage = entry.getValue();
    7. YamlConfiguration config = plugin.getMailConfig();
    8. config.set("Storage." + playerUUID, storage.getStorage());
    9. plugin.saveMailConfig();
    10. }
    11.  
    12.  
    13. }
    14.  
    15.  
    16. }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @xelatercero Entry might not be null, but the value might.
     
  3. Offline

    xelatercero

    @timtower I'm really confused right now , how i should check this ?

    Here for example i get always "there arent null values":

    Code:java
    1. for(Entry<UUID, MailStorage> entry : mailStorage.entrySet()) {
    2. if(entry != null) {
    3. UUID playerUUID = entry.getKey();
    4. MailStorage storage = entry.getValue();
    5. YamlConfiguration config = plugin.getMailConfig();
    6. int nulls = 0;
    7.  
    8. for(HashMap<String, List<ItemStack>> map : storage.getStorage()) {
    9.  
    10. for(Entry<String, List<ItemStack>> en : map.entrySet()) {
    11. if(en.getValue() == null) {
    12. nulls++;
    13. }
    14. }
    15.  
    16. }
    17.  
    18. if(nulls > 0) {
    19. Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "there are null values");
    20. } else {
    21. Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "there arent null values");
    22. }
    23.  
    24.  
    25.  
    26. //config.set("Storage." + playerUUID, storage.getStorage());
    27. //plugin.saveMailConfig();
    28. }
    29.  
    30.  
    31. }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @xelatercero Have you tried printing what you are trying to save?
     
  5. Offline

    xelatercero

    @timtower ok i'm a dummy, i just made the solution in the code above, just touched it a bit:

    Code:java
    1. public void serializeStorage() {
    2.  
    3. for(Entry<UUID, MailStorage> entry : mailStorage.entrySet()) {
    4. if(entry != null) {
    5. UUID playerUUID = entry.getKey();
    6. MailStorage storage = entry.getValue();
    7. YamlConfiguration config = plugin.getMailConfig();
    8. int nulls = 0;
    9.  
    10. for(HashMap<String, List<ItemStack>> map : storage.getStorage()) {
    11.  
    12. for(Entry<String, List<ItemStack>> en : map.entrySet()) {
    13. if(en.getValue() == null) {
    14. nulls++;
    15. }
    16. }
    17.  
    18. }
    19.  
    20. if(nulls == 0) {
    21. config.set("Storage." + playerUUID, storage.getStorage());
    22. plugin.saveMailConfig();
    23. }
    24.  
    25. }
    26.  
    27.  
    28. }
    29.  
    30.  
    31. }
     
Thread Status:
Not open for further replies.

Share This Page