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):
      @EventHandler
        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"))
                            e.setCancelled(true);
                        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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2436) ~[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(PlayerConnectionUtils.java:28) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[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 net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1061) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1054) ~[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(MinecraftServer.java:1038) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:970) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:273) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.NullPointerException: ItemStack can't be null/Air!
            at de.tr7zw.nbtapi.NBTItem.<init>(NBTItem.java:42) ~[?:?]
            at de.tr7zw.nbtapi.NBTItem.<init>(NBTItem.java:28) ~[?:?]
            at net.ultibuild.gui.NBT.hasNBT(NBT.java:23) ~[?:?]
            at net.ultibuild.gui.GUIClickEvent.OnInvAction(GUIClickEvent.java:29) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[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: https://www.spigotmc.org/resources/nbt-api.7939/
    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:
    https://www.spigotmc.org/resources/nbt-api.7939/
    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