Solved Click event returns null even with null check

Discussion in 'Spigot Plugin Development' started by CoderBoyo, Jun 19, 2021.

  1. So this is the code:
    Code (Text):
        public void OnInvAction(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if(e.getCurrentItem() != null) {
                ItemStack item = e.getCurrentItem();
                String allnbt = "";
                if(e.getCurrentItem() != null) {
                    if (NBT.hasNBT(item, "ALLOW_CLICK")) { (this line has error)
                        if (!NBT.getNBTBool(item, "ALLOW_CLICK"))
                        p.sendMessage("click stop");
    (I will not be showing any more of this class)
    Note i added a double check to make sure its not null, this is the error i get:
    Code (Text):
     Could not pass event InventoryClickEvent to MegaSurvival v1
    org.bukkit.event.EventException: null
            at$1.execute( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.callEvent( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:32) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$0( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at [?:?]
    Caused by: java.lang.NullPointerException: ItemStack can't be null/Air!
            at de.tr7zw.nbtapi.NBTItem.<init>( ~[?:?]
            at de.tr7zw.nbtapi.NBTItem.<init>( ~[?:?]
            at net.ultibuild.gui.NBT.hasNBT( ~[?:?]
            at net.ultibuild.gui.GUIClickEvent.OnInvAction( ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke( ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:?]
            at java.lang.reflect.Method.invoke( ~[?:?]
            at$1.execute( ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            ... 18 more
    All I doing is clicking on the item to pick it up that gives to errors but when i try to put it back in any slot this error happens
  2. I'll check your problem now, but before that, please name your functions in a lowerCamelCase
  3. Try this:
    Code (Java):
    if (item != null && item.getType() != Material.AIR) {}
    The error says that it cannot be null or air, so add an air check as well. If this isn't the issue, can you tell us exactly which line the npe is being thrown on?
    • Agree Agree x 1
  4. He's shown the line that throws the NPE - it's the NBT#hasNBT.
    By the way @CoderBoyo , I don't really remember a class called NBT (I might be wrong), so if it's a custom class it'd be cool to show it to us.
    • Agree Agree x 1
  5. It looks to be this:
    If that is the line that the error is being thrown on, then the object should never be null, and I suspect the issue is either the item being air, or an attribute of the item is null.
    • Like Like x 1
  6. as @DJCowGaming said I use this api:
    With a custom wrapper to make the api easier to use
  7. PersistentDataContainer is better alternative to NBT, and it's part of Spigot rather than any external library or API. It also stores persistent data per-plugin, whereas NBT data can be manipulated by any plugin. Just a suggestion though.

    The first thing that happens is that an NBTItem is initialized, and the constructor asserts that it is neither air nor null, else it throws a NullPointerException. The item is definitely air, so just add that air check that DJCowGaming mentioned.
    • Like Like x 1
  8. Thanks to everyone that helped for some reason i thought that the null checked also checked if its air thanks for all the help!
    • Friendly Friendly x 1