Solved NullPointerException on PlayerInteractEvent

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

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


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

    [06:07:50 ERROR]: Could not pass event PlayerInteractEvent to SkyblockSMP v0.7.3
            at$1.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_211]
            at 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( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at [spigot.jar:git-Spigot-db6de12-18fbb24]
            at Source) [?:1.8.0_211]
    Caused by: java.lang.NullPointerException
            at ~[?:?]
            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$1.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 17 more
     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;
                                        p.sendMessage(ChatUtils.format("&aUsed &6" + itemab.getAbilityName() + "&a! &b(" + itemab.getAbilityManaCost() + " Mana)"));
  2. Offline


    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


    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:
    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


    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