Error InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by xForcer9121, May 18, 2017.

  1. Hi! I got a problem. I get a Null pointer exception error when a player clicks in a opened inventory.
    Hope you can help me :)!
     
  2. Can I see your code, and the stacktrace please?
     
  3. Code (Text):
    [12:44:08 ERROR]: Could not pass event InventoryClickEvent to Test v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1849) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            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_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-3fb9445-2b6c9f4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at us.xforcer.test.Main.onClick(Main.java:34) ~[?:?]
            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.jar:git-Spigot-3fb9445-2b6c9f4]
            ... 15 more
    Code (Text):

     @EventHandler
        public void onClick(InventoryClickEvent event){
            Bukkit.broadcastMessage(event.getCurrentItem().getItemMeta().getDisplayName());
        }
     
  4. Please stop spoonfeeding with wrong information. Why are you checking if the even is null.. Obviously the getDisplayName is null not the even.
    Stop spoonfeeding wrong information..
     
  5. My bad man. Wasn't thinking properly...

    It would be this.. no?

    Code (Text):
     @EventHandler
        public void onClick(InventoryClickEvent event){
            if (!(event.getCurrentItem().getItemMeta().getDisplayName()  == null)) {
                Bukkit.broadcastMessage(event.getCurrentItem().getItemMeta().getDisplayName());
            }
        }
    or could you even just use

    Code (Text):
    if (!(event.getCurrentItem() == null)) {
     
  6. First check if itemMeta isn't null than check if displayname isn't null and... poof magic
     
    • Like Like x 1
  7. Additional, You have to check the inventory name if you are assuming that this event will run in certain inventory only second just like what @TheBlackTeddy said you have to check the current item's itemmeta is null and check if the displayname is null if necessary hope it helped :)
     
  8. if(!(event.getCurrentItem() == null)){
    if(event.getCurrentItem().hasItemMeta()){
    Bukkit.broadcastMessage(event.getCurrentItem.getItemMeta().getDisplayName());
    }
    }

    I am not writing on Eclipse or IntelliJ so maybe be wrong but this must be work.