Solved Check for uuid in config returs null

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

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

    xelatercero

    So I want to check if the config contains the player uuid, but it just returns null:

    Code:java
    1. if(!(mailStorage.containsKey(p.getUniqueId()))) {
    2.  
    3. YamlConfiguration config = plugin.getMailConfig();
    4. if(config.contains("Storage." + p.getUniqueId())) {
    5. System.out.println("it does");
    6. }
    7.  
    8.  
    9. }


    i have the sam exact method onPlayerJoinEvent and it works perfectly.

    Maybe its because the config its not loaded yet?
     
  2. Show us the join event?
     
  3. Offline

    timtower Administrator Administrator Moderator

    @xelatercero What return null? Do you mean nullpointerexception?
     
  4. Haha, do they ever get any other error when something's "wrongly" null?

    [​IMG]
     
  5. Offline

    xelatercero

    @timtower @Pr0totype2

    this is the code called in the join event, and it works

    the think returning me null is when i do:
    PHP:
    if(config.contains("Storage." player.getUniqueId()))

    Code:java
    1. if(!mailStorage.containsKey(player.getUniqueId())) {
    2.  
    3. YamlConfiguration config = plugin.getMailConfig();
    4. if(config.contains("Storage." + player.getUniqueId())) {
    5. List<HashMap<String, List<ItemStack>>> list = (List<HashMap<String, List<ItemStack>>>) config.get("Storage." + player.getUniqueId());
    6. MailStorage storage = new MailStorage();
    7. storage.setStorage(list);
    8. System.out.println("reached");
    9. mailStorage.put(player.getUniqueId(), storage);
    10. } else {
    11. mailStorage.put(player.getUniqueId(), new MailStorage());
    12. }
    13.  
    14. System.out.println("it doesnt");
    15.  
    16.  
    17. }
     
  6. Offline

    timtower Administrator Administrator Moderator

    @xelatercero A contains check returns true or false, not null.
     
  7. if (config.get("Storage." + playerUUID)) doesn't exist, it'll return null.
    If it does exist and contains goes through, it'll return true or false.

    @xelatercero Where are you setting it in the file?
     
  8. Offline

    xelatercero

    @timtower
    Code:
    java.lang.NullPointerException: null
     at me.xelatercero.smi.util.InventoryManager.assingInventoriesOnReload(InventoryManager.java:84)
    
    line 84:
    PHP:
    if(config.contains("Storage." p.getUniqueId()))
    The #assingInventoriesOnReload method is called onEnable()
     
  9. Where do you save stuff in the config?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @xelatercero What is the value of config? Please post the entire class.
     
  11. Offline

    KarimAKL

    Depends on what gets thrown, for example, it could also be an IllegalArgumentException.
     
    zThana likes this.
  12. Offline

    xelatercero

    @timtower just arrived from work

    Code:java
    1. //HashMaps
    2. public static HashMap<Player, MailboxInventory> mailboxInv = new HashMap<Player, MailboxInventory>();
    3. public static HashMap<Player, SendMailInventory> sendMailInv = new HashMap<Player, SendMailInventory>();
    4. public static HashMap<UUID, MailStorage> mailStorage = new HashMap<UUID, MailStorage>();
    5. private static Main plugin;
    6.  
    7. public InventoryManager() {
    8. plugin = Main.getMainInstance();
    9. }
    10.  
    11. //
    12. public static void addPlayer(Player player) {
    13.  
    14. if(!(mailboxInv.containsKey(player))) {
    15. mailboxInv.put(player, new MailboxInventory());
    16. }
    17. if(!(sendMailInv.containsKey(player))) {
    18. sendMailInv.put(player, new SendMailInventory());
    19.  
    20. }
    21. System.out.println(mailStorage);
    22.  
    23. if(!mailStorage.containsKey(player.getUniqueId())) {
    24.  
    25. YamlConfiguration config = plugin.getMailConfig();
    26. if(config.contains("Storage." + player.getUniqueId())) { //HERE WORKS
    27. List<HashMap<String, List<ItemStack>>> list = (List<HashMap<String, List<ItemStack>>>) config.get("Storage." + player.getUniqueId());
    28. MailStorage storage = new MailStorage();
    29. storage.setStorage(list);
    30. System.out.println("reached");
    31. mailStorage.put(player.getUniqueId(), storage);
    32. } else {
    33. mailStorage.put(player.getUniqueId(), new MailStorage());
    34. }
    35.  
    36. System.out.println("it doesnt");
    37.  
    38.  
    39. }
    40.  
    41. }
    42.  
    43.  
    44.  
    45. //creates an instance of all inventories for every player on reload
    46. public static void assingInventoriesOnReload() {
    47.  
    48. Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
    49.  
    50. for(Player p : players) {
    51.  
    52. if(!(mailboxInv.containsKey(p))) {
    53. mailboxInv.put(p, new MailboxInventory());
    54. }
    55. if(!(sendMailInv.containsKey(p))) {
    56. sendMailInv.put(p, new SendMailInventory());
    57. }
    58.  
    59.  
    60. if(!(mailStorage.containsKey(p.getUniqueId()))) {
    61.  
    62. YamlConfiguration config = plugin.getMailConfig();
    63. if(config.contains("Storage." + p.getUniqueId())) { //HERE IT DOESNT
    64. System.out.println("it does");
    65. }
    66.  
    67.  
    68. }
    69.  
    70. }
    71.  
    72. }
    73. }


    #assingInventoriesOnReload() is called onEnable() and maybe the config its not loaded yet?
     
  13. Offline

    timtower Administrator Administrator Moderator

    @xelatercero Get rid of the static and use the constructor to set the right values.
    Then do null checks.
     
Thread Status:
Not open for further replies.

Share This Page