HashMap is null {Help}

Discussion in 'Spigot Plugin Development' started by VinexAx789, Jun 14, 2016.

  1. Problem: My HashMap is null and is not giving the kit to the correct person.

    Code:

    Code (Java):
        public DTP plugin;

        public MainMenu(DTP plugin) {
            this.plugin = plugin;

            inv = Bukkit.getServer().createInventory(null, 9, ChatColor.BLUE + "" + ChatColor.BOLD + "Main Menu");
            teams = Bukkit.getServer().createInventory(null, 18, ChatColor.RED + "" + ChatColor.BOLD + "Team Menu");
            kits = Bukkit.getServer().createInventory(null, 18, ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "Kit Menu");

            team = createTeamItem(DyeColor.RED, ChatColor.BLUE + "Teams");
            kititem = createItem(Material.DIAMOND_CHESTPLATE, ChatColor.YELLOW + "Kits");
            back = createItem(Material.BARRIER, ChatColor.RED + "Back");

            attackers = createTeamItem(DyeColor.RED, ChatColor.RED + "Attackers");
            defenders = createTeamItem(DyeColor.BLUE, ChatColor.BLUE + "Defenders");

            kitattackers = createItem(Material.IRON_SWORD, ChatColor.RED + "Kit Attacker");
            kitdefenders = createItem(Material.IRON_CHESTPLATE, ChatColor.RED + "Kit Defender");
            kitarcher = createItem(Material.BOW, ChatColor.RED + "Kit Archer");

            autoselect = createItem(Material.NAME_TAG, ChatColor.RED + "Auto Select");

            inv.setItem(3, team);
            inv.setItem(5, kititem);

            kits.setItem(3, kitattackers);
            kits.setItem(4, kitarcher);
            kits.setItem(5, kitdefenders);
            kits.setItem(17, back);

            teams.setItem(2, attackers);
            teams.setItem(4, autoselect);
            teams.setItem(6, defenders);
            teams.setItem(17, back);
            kitselected = new HashMap<Player, KitType>();

        }

        public Inventory inv, teams, kits;
        public HashMap<Player, KitType> kitselected;
        private ItemStack kititem, team, back, attackers, defenders, kitattackers, kitdefenders, autoselect, kitarcher;
     

    Code (Java):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
            if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())
                    && !e.getInventory().getName().equalsIgnoreCase(teams.getName())
                    && !e.getInventory().getName().equalsIgnoreCase(kits.getName()))
                return;
            if (e.getCurrentItem().getItemMeta() == null)
                return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Teams")) {
                e.setCancelled(true);
                player.sendMessage(plugin.prefix + "Opening team menu.");
                player.closeInventory();
                player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                player.openInventory(teams);
            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kits")) {
                e.setCancelled(true);
                player.sendMessage(plugin.prefix + "Opening kit menu.");
                player.closeInventory();
                player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                player.openInventory(kits);
            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Back")) {
                e.setCancelled(true);
                player.sendMessage(plugin.prefix + "Going back to main menu.");
                player.closeInventory();
                player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                player.openInventory(inv);

            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kit Attacker")) {
                e.setCancelled(true);
                if (!(plugin.getPlayersTeam(player) == plugin.getRed())
                        || !(plugin.getPlayersTeam(player) == plugin.getBlue())) {
                    player.sendMessage(plugin.prefix + "Either pick a kit or get one automatically when the game starts!");
                    player.closeInventory();
                }
                if (plugin.getPlayersTeam(player) == plugin.getBlue()) {
                    player.sendMessage(plugin.prefix + "Selected Kit Attacker");
                    player.closeInventory();
                    player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                    player.closeInventory();
                } else if (plugin.getPlayersTeam(player) == plugin.getBlue()) {
                    player.sendMessage(plugin.prefix + "You cannot select this kit as a Defender!");
                    player.closeInventory();
                }

            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kit Defender")) {
                e.setCancelled(true);
                if (!(plugin.getPlayersTeam(player) == plugin.getBlue())
                        || !(plugin.getPlayersTeam(player) == plugin.getRed())) {
                    player.sendMessage(plugin.prefix + "Either pick a kit or get one automatically when the game starts!");
                    player.closeInventory();
                }
                if (plugin.getPlayersTeam(player) == plugin.getBlue()) {
                    player.sendMessage(plugin.prefix + "Selected Kit Defender");
                    player.closeInventory();
                    player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                    player.closeInventory();
                } else if (plugin.getPlayersTeam(player) == plugin.getRed()) {
                    player.sendMessage(plugin.prefix + "You cannot select this kit as a Attacker!");
                    player.closeInventory();
                }

            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kit Archer")) {
                e.setCancelled(true);
                player.sendMessage(plugin.prefix + "Selected Kit Archer");
                kitselected.remove(player);
                kitselected.put(player, KitType.ARCHER);
                player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                player.closeInventory();
            }

        }

        public KitType getKit(Player player) {
            if (kitselected.containsKey(player)) {
                return kitselected.get(player);
            }
            return KitType.DEFAULT;
        }

        public void giveSelectedKit(final Player p) {
            if (getKit(p).equals(KitType.DEFAULT)) {
                if (plugin.getPlayersTeam(p) == plugin.getRed()) {
                    ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
                    ItemMeta swordmeta = sword.getItemMeta();
                    swordmeta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
                    swordmeta.setDisplayName(ChatColor.RED + p.getName() + "'s sword");
                    sword.setItemMeta(swordmeta);
                    p.getInventory().setItem(0, sword);
                    swordmeta.spigot().setUnbreakable(true);

                    ItemStack pickaxe = new ItemStack(Material.STONE_PICKAXE, 1);
                    ItemMeta pickaxeMeta = pickaxe.getItemMeta();
                    p.getInventory().setItem(3, pickaxe);
                    pickaxeMeta.spigot().setUnbreakable(true);

                    ItemStack grenade = new ItemStack(Material.TNT, 2);
                    ItemMeta grenademeta = grenade.getItemMeta();
                    grenademeta.addEnchant(Enchantment.DIG_SPEED, 1, true);
                    grenademeta.setDisplayName(ChatColor.RED + p.getName() + "'s Grenades " + ChatColor.DARK_GRAY + "("
                            + ChatColor.YELLOW + "Left Click" + ChatColor.DARK_GRAY + ")");
                    grenade.setItemMeta(grenademeta);
                    p.getInventory().setItem(2, grenade);

                    p.getInventory().setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
                    p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
                    p.getInventory().setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS, 1));
                    p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));

                } else if (plugin.getPlayersTeam(p) == plugin.getBlue()) {
                    ItemStack dsword = new ItemStack(Material.STONE_SWORD, 1);
                    ItemMeta dswordmeta = dsword.getItemMeta();
                    dswordmeta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
                    dswordmeta.setDisplayName(ChatColor.BLUE + p.getName() + "'s sword");
                    dsword.setItemMeta(dswordmeta);
                    dswordmeta.spigot().setUnbreakable(true);
                    p.getInventory().setItem(0, dsword);

                    ItemStack dpickaxe = new ItemStack(Material.STONE_PICKAXE, 1);
                    ItemMeta dpickaxeMeta = dpickaxe.getItemMeta();
                    p.getInventory().setItem(3, dpickaxe);
                    dpickaxeMeta.spigot().setUnbreakable(true);

                    p.getInventory().setItem(3, new ItemStack(Material.GOLDEN_APPLE, 3));

                    ItemStack dgrenade = new ItemStack(Material.TNT, 2);
                    ItemMeta dgrenademeta = dgrenade.getItemMeta();
                    dgrenademeta.addEnchant(Enchantment.DIG_SPEED, 1, true);
                    dgrenademeta.setDisplayName(ChatColor.RED + p.getName() + "'s Grenades " + ChatColor.DARK_GRAY + "("
                            + ChatColor.YELLOW + "Left Click" + ChatColor.DARK_GRAY + ")");
                    dgrenade.setItemMeta(dgrenademeta);
                    p.getInventory().setItem(4, dgrenade);

                    p.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
                    p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
                    p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
                    p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
                }
            } else if (getKit(p).equals(KitType.ARCHER)) {
                ItemStack bow = new ItemStack(Material.BOW, 1);
                ItemMeta bowMeta = bow.getItemMeta();
                bowMeta.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
                bowMeta.spigot().isUnbreakable();

                if (plugin.getPlayersTeam(p) == plugin.getBlue()) {
                    bowMeta.setDisplayName(C.blue + p.getName() + "'s bow");
                } else {
                    bowMeta.setDisplayName(C.red + p.getName() + "'s bow");
                }
                bow.setItemMeta(bowMeta);

                ItemStack sword = new ItemStack(Material.WOOD_SWORD, 1);
                ItemMeta swordMeta = bow.getItemMeta();
                swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
                swordMeta.spigot().isUnbreakable();

                if (plugin.getPlayersTeam(p) == plugin.getBlue()) {
                    swordMeta.setDisplayName(C.blue + p.getName() + "'s sword");
                } else {
                    swordMeta.setDisplayName(C.red + p.getName() + "'s sword");
                }
                sword.setItemMeta(swordMeta);

                p.getInventory().setItem(0, sword);
                p.getInventory().setItem(1, bow);
                p.getInventory().setItem(9, new ItemStack(Material.ARROW, 1));
                p.getInventory().setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
                p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
                p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
                p.getInventory().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS, 1));
            }
        }
     
  2. Could you show which line is giving the NPE?
     
  3. you can give me the error?
     
  4. The thing is it's not printing out in console it's not giving me an NPE I just did some debugging and it printed out that it was null also the method that I have its returning default if the player doesn't have a kit but when they do its null when it shouldn't be.
     
  5. Well, you HashMap in your MainMenu object never has anything put into it.
     
  6. What do you mean? Sorry I couldn't really understand.
     
  7. Code (Text):
    public HashMap<Player, KitType> kitselected;
    You never do anything with it.
     
  8. I had something similar to this, it wouldn't put out an error, but it acted as if it never existed. You are probably enabling that class twice in the amount of time between opening the menu and clicking on the item. In that case, it will re-create the hashmap and remove everything in it.
     
    • Like Like x 1
  9. Look again :p

    Code (Text):
        public HashMap<Player, KitType> kitselected;
    Code (Text):
            kitselected = new HashMap<Player, KitType>();
     
  10. Strange, is there a way to fix this? Common sense "Find where it's going twice." yeah and all but I'm pretty much clueless on where it's even at.
     
  11. make sure you're adding something to the hashmap lol. You can't just expect an empty hashmap to give you anything. use .put to enter the kit.
     
  12. Lol, look again.

    Code (Text):
            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kit Archer")) {
                e.setCancelled(true);
                player.sendMessage(plugin.prefix + "Selected Kit Archer");
                kitselected.remove(player);
                kitselected.put(player, KitType.ARCHER);
                player.playSound(e.getWhoClicked().getLocation(), Sound.NOTE_PLING, 1, 1);
                player.closeInventory();
            }
    Code (Text):
        public KitType getKit(Player player) {
            if (kitselected.containsKey(player)) {
                return kitselected.get(player);
            }
            return KitType.DEFAULT;
        }

        public void giveSelectedKit(final Player p) {
            if (getKit(p).equals(KitType.DEFAULT)) {
                if (plugin.getPlayersTeam(p) == plugin.getRed()) {
                    ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
                    ItemMeta swordmeta = sword.getItemMeta();
                    swordmeta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
                    swordmeta.setDisplayName(ChatColor.RED + p.getName() + "'s sword");
                    sword.setItemMeta(swordmeta);
                    p.getInventory().setItem(0, sword);
                    swordmeta.spigot().setUnbreakable(true);

                    ItemStack pickaxe = new ItemStack(Material.STONE_PICKAXE, 1);
                    ItemMeta pickaxeMeta = pickaxe.getItemMeta();
                    p.getInventory().setItem(3, pickaxe);
                    pickaxeMeta.spigot().setUnbreakable(true);

                    ItemStack grenade = new ItemStack(Material.TNT, 2);
                    ItemMeta grenademeta = grenade.getItemMeta();
                    grenademeta.addEnchant(Enchantment.DIG_SPEED, 1, true);
                    grenademeta.setDisplayName(ChatColor.RED + p.getName() + "'s Grenades " + ChatColor.DARK_GRAY + "("
                            + ChatColor.YELLOW + "Left Click" + ChatColor.DARK_GRAY + ")");
                    grenade.setItemMeta(grenademeta);
                    p.getInventory().setItem(2, grenade);

                    p.getInventory().setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
                    p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
                    p.getInventory().setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS, 1));
                    p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));

                } else if (plugin.getPlayersTeam(p) == plugin.getBlue()) {
                    ItemStack dsword = new ItemStack(Material.STONE_SWORD, 1);
                    ItemMeta dswordmeta = dsword.getItemMeta();
                    dswordmeta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
                    dswordmeta.setDisplayName(ChatColor.BLUE + p.getName() + "'s sword");
                    dsword.setItemMeta(dswordmeta);
                    dswordmeta.spigot().setUnbreakable(true);
                    p.getInventory().setItem(0, dsword);

                    ItemStack dpickaxe = new ItemStack(Material.STONE_PICKAXE, 1);
                    ItemMeta dpickaxeMeta = dpickaxe.getItemMeta();
                    p.getInventory().setItem(3, dpickaxe);
                    dpickaxeMeta.spigot().setUnbreakable(true);

                    p.getInventory().setItem(3, new ItemStack(Material.GOLDEN_APPLE, 3));

                    ItemStack dgrenade = new ItemStack(Material.TNT, 2);
                    ItemMeta dgrenademeta = dgrenade.getItemMeta();
                    dgrenademeta.addEnchant(Enchantment.DIG_SPEED, 1, true);
                    dgrenademeta.setDisplayName(ChatColor.RED + p.getName() + "'s Grenades " + ChatColor.DARK_GRAY + "("
                            + ChatColor.YELLOW + "Left Click" + ChatColor.DARK_GRAY + ")");
                    dgrenade.setItemMeta(dgrenademeta);
                    p.getInventory().setItem(4, dgrenade);

                    p.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
                    p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
                    p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
                    p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
                }
            } else if (getKit(p).equals(KitType.ARCHER)) {
                ItemStack bow = new ItemStack(Material.BOW, 1);
                ItemMeta bowMeta = bow.getItemMeta();
                bowMeta.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
                bowMeta.spigot().isUnbreakable();

                if (plugin.getPlayersTeam(p) == plugin.getBlue()) {
                    bowMeta.setDisplayName(C.blue + p.getName() + "'s bow");
                } else {
                    bowMeta.setDisplayName(C.red + p.getName() + "'s bow");
                }
                bow.setItemMeta(bowMeta);

                ItemStack sword = new ItemStack(Material.WOOD_SWORD, 1);
                ItemMeta swordMeta = bow.getItemMeta();
                swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
                swordMeta.spigot().isUnbreakable();

                if (plugin.getPlayersTeam(p) == plugin.getBlue()) {
                    swordMeta.setDisplayName(C.blue + p.getName() + "'s sword");
                } else {
                    swordMeta.setDisplayName(C.red + p.getName() + "'s sword");
                }
                sword.setItemMeta(swordMeta);

                p.getInventory().setItem(0, sword);
                p.getInventory().setItem(1, bow);
                p.getInventory().setItem(9, new ItemStack(Material.ARROW, 1));
                p.getInventory().setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
                p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
                p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
                p.getInventory().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS, 1));
            }
        }
    .put has been used :p
     
  13. Well, okay, I know this isn't the best way/"practice" of doing things, but it works the same.

    So you have a constructor for each one of your classes: One in Main, Menus, and Events.

    So, InvClickEvent is in the events class and handles all click events. Main handles all the main stuff :p. And your Menus creates all the menus.

    So your constructor looks like this I assume..
    Code (Text):
    Main plugin;

    public <ClassName>(Main pl) {
    plugin = pl;
    }
    Well, when you are creating all the hashmaps in the OnEnable in the Main class the constructor is called.
    But, when you open the inventory you are "added" to the map theoretically. But when you click on the inventory, it ends up null because you did this in your events class:

    Code (Text):
    Main plugin;

    Menus menu;

    public Events(Main pl) {
    plugin = pl;

    menu = new Menus(plugin);
    }
    Therefore you just recreated the hashmap.
    That's the thing that I found out to be wrong. The way I got around it is this.

    Only add your constructor with enabling the different classes in the main. Not in other classes. To get the Menus class in the Events class simply do
    Code (Text):
    plugin.menu.<whateverMethod>
    Again, not the best way of doing it, but it works.
     
  14. That's not what I did but thanks for the help a bit :p I'm only using the hashmap in this class only and no other.
     
  15. Are you using the Menus class in other classes? Like calling methods?
     
  16. I'm using the menu class to call the giveSelectedKit method.
     
  17. Alright, then it might not matter then. :p
     
    • Funny Funny x 1