Solved NullPointer exception

Discussion in 'Spigot Plugin Development' started by SimPlaysGames, May 11, 2016.

  1. Hey, I am getting a nullpointer exception. It happens when I click somewhere else then on the items in the menu. How would I fix this?


    Code on Inventory click
    http://pastebin.com/bqK82inK

    Code (Text):
    [21:29:56 ERROR]: Could not pass event InventoryClickEvent to MaximizedClasses v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1852) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
            at me.spigot.SimPlaysGames.Event.Player.InventoryClick.onInventoryClick(InventoryClick.java:57) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_65]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.9.2.jar:git-Spigot-5a40365-b70058a]
            ... 15 more
     
  2. Check in the event if clickedItemstack != null
     
    • Agree Agree x 2
  3. Ok, so incase the if statements in that and what do I do in the else statement
     
  4. Item is null so add
    Code (Text):
    if(item==null)
        return;
    , also thats not how you compare strings:
    Code (Text):
    item.getItemMeta().getDisplayName() == Menu.className
    use
    Code (Text):
    && item.getItemMeta().getDisplayName() .equals( Menu.className)
    instead
     
    • Agree Agree x 1