1.8.8 NullPointerException

Discussion in 'Spigot Plugin Development' started by TL_Gaming, Feb 8, 2020.

  1. So I have a problem with my code here. I tried looking for answers on all kind of forums etc, without any succes.

    I am still learning how to code plugins (my java knowledge isn't fantastic) so I don't really know where I made a mistake.

    The Console gave this error:
    Code (Text):
    [14:50:56 ERROR]: Could not pass event InventoryClickEvent to LadsCraftStaff v1.0.2
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at me.thomas.ladscraftstaff.events.BanInventoryEvent.onMenuClick(BanInventoryEvent.java:33) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 15 more
    I know there is a mistake on line 33 of my BanInventoryEvent class but I really don't know what it is and how I could resolve it.
    (Line 33 is at the If statement: if(e.getCurrentItem().getType() == Material.SKULL_ITEM) )

    This is the code where the problem is:
    Code (Java):
    package me.thomas.ladscraftstaff.events;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;

    import me.thomas.ladscraftstaff.Main;
    import me.thomas.ladscraftstaff.utils.Utils;



    public class BanInventoryEvent implements Listener {
       
    private Main plugin;
       
        public BanInventoryEvent(Main plugin) {
        this.plugin = plugin;
        Bukkit.getServer().getPluginManager().registerEvents(this, plugin);;
        }

       
        @EventHandler
        public void onMenuClick(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
           
           
           
            if(e.getInventory().getTitle().equalsIgnoreCase(Utils.chat(plugin.getConfig().getString("BanGUI.gui_title")))) {
               
                if(e.getCurrentItem().getType() == Material.SKULL_ITEM) {
                   
                   
                    Player whoToBan = player.getServer().getPlayer(e.getCurrentItem().getItemMeta().getDisplayName());
                   
                    plugin.openConfirmGUI(player, whoToBan);
                   
                }
            }
           
            e.setCancelled(true); //Makes items unmoveable
        }
    }
    This is the code where I created the SKULL_ITEM ItemStack etc:
    Code (Java):
    if(sender instanceof Player) {
                Player player = (Player) sender;
               
               
                ArrayList<Player> player_list = new ArrayList<>(player.getServer().getOnlinePlayers());
               
                Inventory bangui = Bukkit.createInventory(player, 45, Utils.chat(plugin.getConfig().getString("BanGUI.gui_title")));
               
                //For every player, add their name and head to the gui
                for(int i = 0; i < player_list.size(); i++) {
                    ItemStack playerHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                    ItemMeta meta = playerHead.getItemMeta();
                    meta.setDisplayName(Utils.chat(plugin.getConfig().getString("BanGUI.playerheaddisplayname_color")) + player_list.get(i).getDisplayName());
                   
                    ArrayList<String> lore = new ArrayList<>();
                    lore.add(Utils.chat(plugin.getConfig().getString("BanGUI.playerhealth_lore")) + player_list.get(i).getHealth());
                    lore.add(Utils.chat(plugin.getConfig().getString("BanGUI.playerexperience_lore")) + player_list.get(i).getExp());
                    meta.setLore(lore);
                   
                    playerHead.setItemMeta(meta);
                   
                   
                   
                    bangui.addItem(playerHead);
                }
               
                player.openInventory(bangui);
    If anyone knows how I could fix this, please help me :rolleyes:
    Thanks for taking the time to read this and help me ;)
     
  2. I haven't really used this part of the API, but I would suggest trying this
    Code (Java):

    if (e.getCurrentItem().getType() != null && e.getCurrentItem().getType() == Material.SKULL_ITEM) {
        // Your code here
    }
     
    UPDATE:
    I would suggest OP check out this thread here on the Bukkit forums, this solution is similar to what I suggested.

    https://bukkit.org/threads/inventoryclickevent-null-proplem.174817/
     
  3. I am 100% sure that e.getCurrentItem() is null, so calling a method on a null object is going to throw an error. This is probably from a player clicking on an empty slot in an inventory.

    1. You should check if that item is null
    2. You should learn some basics of java, like reading errors.
      1. https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it
      2. If you get an error you don't understand, just google it. Many people will have asked the question before

    That code will still throw the error because you are calling a method on a null object.
     
    • Agree Agree x 2
  4. Thank you for the reply! I'll have a look in the morning. I tried to google the problem, but the results didn't really help me out so ye.
    As I said tho, my java knowledge isn't fantastic I'm trying to learn it a bit by following tutorials etc.

    Again, thanks for the help!
     
  5. Choco

    Moderator

    10 bucks says you'll get another NPE if you click outside of the inventory window. You should null check e.getInventory() as well because it can return null.
     
    • Like Like x 4
  6. Yep, just realized that @TL_Gaming you will have to do a null check on the inventory and the current item.
     
    • Like Like x 1