Solved NullPointerException on PlayerInteractEvent

Discussion in 'Plugin Development' started by FoxinatorDev, Dec 14, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    FoxinatorDev

    I keep getting an error whenever I try seeing if the item's display name equals or contains an item name.

    Error:
    Code:
    [06:07:50 ERROR]: Could not pass event PlayerInteractEvent to SkyblockSMP v0.7.3
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_211]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_211]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
    Caused by: java.lang.NullPointerException
            at skyblock.events.PlayerInteract.onPlayerInteract(PlayerInteract.java:53) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_211]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_211]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 17 more
    Code:
    Code:
     ISkyblockItem item = ItemManager.getInstance().getItemFromEnum(sbitem);
                    if(e.getItem() != null || e.getItem().getType() != Material.AIR) {
                        if (e.getItem().hasItemMeta()) {
                            if(e.getItem().getItemMeta().hasDisplayName()) {
                                ItemMeta meta = e.getItem().getItemMeta();
                                if (meta.getDisplayName().equals(item.getRarity().getColor() + item.getName())) { // Line 53
                                    if (item.hasAbility()) {
                                        IAbility itemab = (IAbility) item;
                                        itemab.ability(SkyblockPlayer.getSBPlayerFromPlayer(p));
                                        p.sendMessage(ChatUtils.format("&aUsed &6" + itemab.getAbilityName() + "&a! &b(" + itemab.getAbilityManaCost() + " Mana)"));
                                    }
                                }
                            }
                        }
                    }
     
  2. There are a few things that could go wrong:
    -meta is null (should not be possible)
    -meta.getDisplayName() returns null (should not be possible)
    -item is null
    -item.getRarity() returns null

    Just print both before line 53 and see which one is null.
     
  3. Offline

    Strahan

    He already did checks to see if there is meta and a displayname, so it is neither of those. Assuming one has grabbed a valid meta (meta can be null but irrelevant as he checked for it), DN can't be null anyway. It's likely item. I'm not familiar with skyblock's API so can't say what can and can't be null for that.

    Add some debug lines as knokko suggested and you won't have to guess:
    Code:
    if (item == null) { e.getPlayer().sendMessage("Item is null"); return; }
    if (item.getRarity() == null) { e.getPlayer().sendMessage("Rarity is null"); return; }
    if (item.getRarity().getColor() == null) { e.getPlayer().sendMessage("Color is null"); return; }
    if (item.getName() == null) { e.getPlayer().sendMessage("Item name is null"); return; }
     
  4. Offline

    FoxinatorDev

    Sorry for really late response, been really sick, but apparently the item was null and got it fixed. Thank you guys for all the help :D
     
Thread Status:
Not open for further replies.

Share This Page