Solved Inventory click event error

Discussion in 'Spigot Plugin Development' started by skeeter, Jan 30, 2020.

  1. i am coding a ban gui plugin but when i tested it i got this error


    upload_2020-1-30_3-43-59.png

    Code (Text):

    public class baninventoryevent implements Listener {

        BanGUI plugin;

        public baninventoryevent(BanGUI plugin) {
            this.plugin = plugin;
        }

        @EventHandler
        public void onMenuClick(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();

            if (e.getClickedInventory().getTitle().equalsIgnoreCase(ChatColor.RED + "BanGUI")) {
                if (e.getCurrentItem().getType() == Material.PLAYER_HEAD) {
                    Player whoToBan = player.getServer().getPlayer(e.getCurrentItem().getItemMeta().getDisplayName());

                    plugin.openBanReason(player, whoToBan);
                }
            }else if (e.getClickedInventory().getTitle().equalsIgnoreCase("Reasons")) {
                switch (e.getCurrentItem().getType()){
                    case IRON_SWORD:
                        String name = e.getClickedInventory().getItem(4).getItemMeta().getDisplayName();
                        player.getServer().getBanList(BanList.Type.NAME).addBan(e.getClickedInventory().getItem(4).getItemMeta().getDisplayName(), "Using combat hacks to an unfair adventage", null, player.getDisplayName());
                        player.sendMessage(ChatColor.RED + "Banned, Thanks for banning and help the server");
                        break;
                    case BARRIER:
                        plugin.openGUI(player);
                        break;
                }
            }


            e.setCancelled(true);
        }
    }


     
    Please help
     
  2. I have done looked at line 33 on the code and i didnt see nothing wornge

    EDITED: i am new to coding so ya dont hurt me
     
  3. Can you send us line 33?
     
  4. String name = e.getClickedInventory().getItem(4).getItemMeta().getDisplayName();

    upload_2020-1-30_4-0-49.png
     
  5. Maybe your Player object is null, because itemMeta#getDisplayName() return the full name of current item, with colored codes and etc.
     
  6. Then getItem returns null (air) or it contains no itemmeta. Please check for null.
     
    #6 stijnb1234, Jan 30, 2020
    Last edited: Jan 30, 2020
  7. Are you sure, that there is an item on that slot, because it seems like there is not or it has no metadata.