Solved NullPointerException on InventoryClickEvent while using switch

Discussion in 'Spigot Plugin Development' started by jonamaster, Jun 26, 2016.

  1. First thread ever on Spigot forums, sorry if I'm doing something terrible like posting where i shouldn't. I need help with the following code (I get a NPE):

    Code (Text):
        //When a player clicks inside an inventory.
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();
         
            //If current inventory is main Market window.
            if (inventory.getName().equals(marketInventory1.getName())){
                event.setCancelled(true);
                switch (clicked.getItemMeta().getDisplayName()){
                case "Bank Menu":
                    player.openInventory(bankInventory1);
                    break;
                case "El Trimendo Item":
                    player.closeInventory();
                    player.getInventory().addItem(customAxe);
                    player.updateInventory();
                    break;
                default:
                    break;
                }
            } else {
                //If current inventory is Bank Menu.
                if (inventory.getName().equalsIgnoreCase("Bank Menu") && clicked != null && clicked.getType() != Material.AIR) {
                    event.setCancelled(true);
                    switch (clicked.getItemMeta().getDisplayName()){
                    case "Sell Iron Ingot":
                        if (player.getInventory().containsAtLeast(new ItemStack (Material.IRON_INGOT), 1)){
                            player.getInventory().removeItem(new ItemStack (Material.IRON_INGOT));
                            balance.put(player.getName(), balance.get(player.getName()) + 100);
                            player.sendMessage(ChatColor.YELLOW+"Sold "+ChatColor.GREEN+"1"+ChatColor.YELLOW+" Iron Ingot for "+ChatColor.GREEN+"100");
                            player.updateInventory();
                        } else {
                            player.sendMessage(ChatColor.RED+"You don't have enough Iron Ingots");
                        }
                        break;
                    case "Sell Gold Ingot":
                        if (player.getInventory().containsAtLeast(new ItemStack (Material.GOLD_INGOT), 1)){
                            player.getInventory().removeItem(new ItemStack (Material.GOLD_INGOT));
                            balance.put(player.getName(), balance.get(player.getName()) + 300);
                            player.sendMessage(ChatColor.YELLOW+"Sold "+ChatColor.GREEN+"1"+ChatColor.YELLOW+" Gold Ingot for "+ChatColor.GREEN+"300");
                            player.updateInventory();
                        } else {
                            player.sendMessage(ChatColor.RED+"You don't have enough Gold Ingots");
                        }
                        break;
                    case "Sell Diamond":
                        if (player.getInventory().containsAtLeast(new ItemStack (Material.DIAMOND), 1)){
                            player.getInventory().removeItem(new ItemStack (Material.DIAMOND));
                            balance.put(player.getName(), balance.get(player.getName()) + 1000);
                            player.sendMessage(ChatColor.YELLOW+"Sold "+ChatColor.GREEN+"1"+ChatColor.YELLOW+" Diamond for "+ChatColor.GREEN+"1000");
                            player.updateInventory();
                        } else {
                            player.sendMessage(ChatColor.RED+"You don't have enough Diamonds");
                        }
                        break;
                    default:
                        break;
                    }
                }
            }
        }

    It says the line causing the problem is line 180:
    Code (Text):
    switch (clicked.getItemMeta().getDisplayName()){
     
    I really try to search and solve all by myself, but this time i can't figure what's causing my problem. It works but floods console, that's the real problem. Thank you very much in advance!
     
    #1 jonamaster, Jun 26, 2016
    Last edited: Jun 27, 2016
  2. Item meta and the display name can be null (if the name is the mc default) so you will need a null check before it.
     
  3. Oh wow, i didn't know that MC default names where considered null, I'll try now with that in mind. Thanks!
     
    • Like Like x 1
    • Informative Informative x 1
  4. Fixed code:
    Code (Text):
        //When a player clicks inside an inventory.
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getClickedInventory();
            //Check inventory is not null (Clicked out of inventory bounds).
            if (inventory == null) return;
         
            //If current inventory is main Market window.
            if (inventory.getName().equals(marketInventory1.getName()) && clicked.hasItemMeta() && clicked.getItemMeta().hasDisplayName() && clicked.getType() != Material.AIR){
                event.setCancelled(true);
                switch (clicked.getItemMeta().getDisplayName()){
                case "Bank Menu":
                    player.openInventory(bankInventory1);
                    break;
                case "El Trimendo Item":
                    player.closeInventory();
                    player.getInventory().addItem(customAxe);
                    player.updateInventory();
                    break;
                default:
                    break;
                }
            } else {
                //If current inventory is Bank Menu.
                if (inventory.getName().equals(bankInventory1.getName()) && clicked.hasItemMeta() && clicked.getItemMeta().hasDisplayName() && clicked.getType() != Material.AIR) {
                    event.setCancelled(true);
                    switch (clicked.getItemMeta().getDisplayName()){
                    case "Sell Iron Ingot":
                        if (player.getInventory().containsAtLeast(new ItemStack (Material.IRON_INGOT), 1)){
                            player.getInventory().removeItem(new ItemStack (Material.IRON_INGOT));
                            balance.put(player.getName(), balance.get(player.getName()) + 100);
                            player.sendMessage(ChatColor.YELLOW+"Sold "+ChatColor.GREEN+"1"+ChatColor.YELLOW+" Iron Ingot for "+ChatColor.GREEN+"100");
                            player.updateInventory();
                        } else {
                            player.sendMessage(ChatColor.RED+"You don't have enough Iron Ingots");
                        }
                        break;
                    case "Sell Gold Ingot":
                        if (player.getInventory().containsAtLeast(new ItemStack (Material.GOLD_INGOT), 1)){
                            player.getInventory().removeItem(new ItemStack (Material.GOLD_INGOT));
                            balance.put(player.getName(), balance.get(player.getName()) + 300);
                            player.sendMessage(ChatColor.YELLOW+"Sold "+ChatColor.GREEN+"1"+ChatColor.YELLOW+" Gold Ingot for "+ChatColor.GREEN+"300");
                            player.updateInventory();
                        } else {
                            player.sendMessage(ChatColor.RED+"You don't have enough Gold Ingots");
                        }
                        break;
                    case "Sell Diamond":
                        if (player.getInventory().containsAtLeast(new ItemStack (Material.DIAMOND), 1)){
                            player.getInventory().removeItem(new ItemStack (Material.DIAMOND));
                            balance.put(player.getName(), balance.get(player.getName()) + 1000);
                            player.sendMessage(ChatColor.YELLOW+"Sold "+ChatColor.GREEN+"1"+ChatColor.YELLOW+" Diamond for "+ChatColor.GREEN+"1000");
                            player.updateInventory();
                        } else {
                            player.sendMessage(ChatColor.RED+"You don't have enough Diamonds");
                        }
                        break;
                    default:
                        break;
                    }
                } else {
                    event.setCancelled(true);
                    return;
                }
            }
        }

    Thank you again for your help @Lyxnx, and if someone still can't solve something like what happened to me, check this! (it clarified a little thing that i didn't know): https://www.spigotmc.org/threads/help-with-inventoryclickevent.142836/#post-1514126