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
        at$1.execute( ~[spigot.jar:git-Spigot-c3e4052-1953f52]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-c3e4052-1953f52]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-c3e4052-1953f52]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a( [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$ [spigot.jar:git-Spigot-c3e4052-1953f52]
        at java.util.concurrent.Executors$ [?:1.8.0_92]
        at [?: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( [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D( [spigot.jar:git-Spigot-c3e4052-1953f52]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C( [spigot.jar:git-Spigot-c3e4052-1953f52]
        at [spigot.jar:git-Spigot-c3e4052-1953f52]
        at [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
        at me.MaxKrissigo.Firework.SavedFirework.savedFirework( ~[?:?]
        at sun.reflect.GeneratedMethodAccessor248.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_92]
        at java.lang.reflect.Method.invoke( ~[?:1.8.0_92]
        at$1.execute( ~[spigot.jar:git-Spigot-c3e4052-1953f52]
        ... 15 more

    Code (Text):
            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) {
                    settings.getSavedFireworks().set(p.getName() + ".selectedFirework", e.getCurrentItem().getItemMeta().getDisplayName().substring(2));
    #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):
    not getName().
  4. me.MaxKrissigo.Firework.SavedFirework.savedFirework( ~[?:?]
    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.