return null in hashmap

Discussion in 'Plugin Development' started by Kolia1_1, Mar 17, 2019.

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

    Kolia1_1

    PHP:
    public class MainGUI implements Listener {

        private 
    HashMap<PlayerInventorymap = new HashMap<>();

        public 
    void onLoad(Player player) {
            
    map.put(playerBukkit.createInventory(null6));
            
    player.openInventory(getInventory(player));
            
    Bukkit.broadcastMessage(map.toString());
        }

        @
    EventHandler
        
    private void onClick(InventoryClickEvent event) {
            
    Player player = (Playerevent.getWhoClicked();
            
    player.sendMessage(String.valueOf(getInventory(player))); //return here null :(
        
    }

        private 
    Inventory getInventory(Player player) {
            return 
    map.get(player);
        }
    }
    return null in clicked slot
     
  2. Offline

    timtower Ninja on the waves Moderator

    @Kolia1_1 1. Please use UUID's instead of Player objects.
    2. A map will return null when the key is not found. Player objects are very tricky with that (henge 1)
     
  3. Offline

    Shqep

    Solved yet?
     
  4. Offline

    Eccentric_Gamer

    Or you have to make sure that players that are looked for were added with a value at some point, possibly using PlayerJoinEvent.

    But UUID's are a good idea.
     
  5. Offline

    Robnoo

    Use immutable (unchangeable) objects as HashMap keys (UUID is the best choice here).
    Also, check if the HashMap contains the player.
    if(map.contains(player.getUUID()){}
     
Thread Status:
Not open for further replies.

Share This Page