Solved InventoryClickEvent NPE

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, Jun 27, 2016.

  1. I'm constantly getting an NPE on the line of code below whenever I click anything inventory related and have no idea why it is occurring. Here is the stacktrace and the relevant code:

    Code (Text):
    [21:02:12 ERROR]: Could not pass event InventoryClickEvent to Firework v2.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-c3e4052-1953f52]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-c3e4052-1953f52]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1853) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-c3e4052-1953f52]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
        at me.MaxKrissigo.Firework.SavedFirework.savedFirework(SavedFirework.java:393) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor248.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-c3e4052-1953f52]
        ... 15 more
     

    Code (Text):
            @EventHandler
            public void savedFirework(InventoryClickEvent e) {
                Player p = (Player) e.getWhoClicked();
                if (!e.getInventory().getName().equalsIgnoreCase(savedFirework.getName())) return; //line 393
                if (!e.getCurrentItem().hasItemMeta()) return;
                if (e.getCurrentItem().getType() == Material.FIREWORK) {
                    e.setCancelled(true);
                    p.openInventory(selection);
                    settings.getSavedFireworks().set(p.getName() + ".selectedFirework", e.getCurrentItem().getItemMeta().getDisplayName().substring(2));
                    settings.saveSavedFireworks();
                }
            }
     
    #1 MaxKrissigo, Jun 27, 2016
    Last edited: Jun 27, 2016
  2. Whats line 393 of SavedFirework class?
     
  3. Short guess is, I think you wanted to grab the Title of the Inventory. Well, for this you need:

    Code (Text):
    e.getInventory().getTitle()
    not getName().
     
  4. me.MaxKrissigo.Firework.SavedFirework.savedFirework(SavedFirework.java:393) ~[?:?]
    What line is this?
     
  5. I should have made it a bit clearer, it is commented next to the line of code //line 393
     
  6. savedFirework.getName() is null or getName returns null
    USe getTitle and try that
     
  7. Same error.
     
  8. Debugged the inventory name and title? maybe savedFirework is null, debug that too.
     
  9. savedFirework works all fine. Don't see any reason why it would be null, if it was it would probably break all of my code xD.
     
  10. Just debug it ;) more results are better.
     
  11. It turned out to be null and I figured it out. It had to do with the name including the player's name. Thanks for the help!
     
    • Like Like x 1
  12. Use InventoryHandler instead.