Solved NullPointerException on InventoryClickEvent

Discussion in 'Plugin Development' started by ramptoetsenbord, Jul 30, 2017.

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

    ramptoetsenbord

    I am making a plugin just to learn and I am at the part where I want that if you click on a sign that there pops up an inventory and that you than can select your kit. the inventory pop up works but the kit selection doesn 't work.

    i get this error:
    Code:
    Could not pass event InventoryClickEvent to TestPlugin vBeta1.9.1
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1877) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at me.bukkit.firstFlame.events.inventory.InventoryClick.onInventoryClick(InventoryClick.java:30) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            ... 15 more
    and this is my onInventoryClickEvent:
    Code:
    package me.bukkit.firstFlame.events.inventory;
    
    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 org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class InventoryClick implements Listener {
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
    
            Player player = (Player) event.getWhoClicked();
            Inventory PlayerInv = player.getInventory();
            Inventory inv = event.getClickedInventory();
    
            player.sendMessage("stap1");
    
            if (inv.getName().equalsIgnoreCase("Kit")) {
    
                player.sendMessage("stap2");
    
                ItemStack item = event.getCurrentItem();
              
                if (item != null) {
                  
                    event.setCancelled(true);
                  
                    if (item.getItemMeta().getDisplayName().equalsIgnoreCase("starter kit")) {
    
                        player.sendMessage("stap3");
    
                        ItemStack food = new ItemStack(Material.COOKED_BEEF, 6);
                        ItemStack ChestArmor = new ItemStack(Material.LEATHER_CHESTPLATE);
                        ItemStack sword = new ItemStack(Material.STONE_SWORD);
    
                        PlayerInv.clear();
                        PlayerInv.addItem(food, sword, ChestArmor);
    
                    }
                }
            }
    
        }
    
    }
    

    If there is someone who could help, that would be nice!
     
    Last edited: Jul 30, 2017
  2. Offline

    yPedx

  3. Offline

    ramptoetsenbord

    line 30 is:
    Code:
    if (item.getItemMeta().getDisplayName().equalsIgnoreCase("starter kit")) {
    }
    i think this is ok and that it works

    srry for the delay my messages get delayd until there is a moderator

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 30, 2017
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    ramptoetsenbord

    thank you!
    this works indeed!
    just added in a null checker and everithing works!
    :p
     
    Last edited: Aug 1, 2017
Thread Status:
Not open for further replies.

Share This Page