Solved ItemStack returning null

Discussion in 'Spigot Plugin Development' started by IcyFlameX, Jun 30, 2020.

Thread Status:
Not open for further replies.
  1. Code (YAML):
    [15:35:53] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to GTACops v2.3.1
    org.bukkit.event.EventException
    : null
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1618]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:236) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:203) ~[patched_1.12.2.jar:git-Paper-1618]
        at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:199) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1530) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:24) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:5) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_251]
        at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_251]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by
    : java.lang.NullPointerException
        at me.IcyFlameX.GTACops.listenerPackage.ListenerClass.onMouseClick(ListenerClass.java:147) ~[?:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor24.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[patched_1.12.2.jar:git-Paper-1618]
        ... 18 more
    Line 147:
    Code (Java):
    if (item != null && item.getItemMeta().getDisplayName().equals(ChatColor.translateAlternateColorCodes('&',
                    plugin.getConfigFileManager().getMsgConfigFile().getString("GTACops_Gui.MainPanel.Compass.Name") + ""))) {
    Function:
    Code (Java):
     @EventHandler
        public void onMouseClick(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
            if (item != null && item.getItemMeta().getDisplayName().equals(ChatColor.translateAlternateColorCodes('&',
                    plugin.getConfigFileManager().getMsgConfigFile().getString("GTACops_Gui.MainPanel.Compass.Name") + ""))) {
                if (action.equals(Action.LEFT_CLICK_AIR) || action.equals(Action.LEFT_CLICK_BLOCK)) {
                    if (tracker.getDistOfNearestWanted(player) != -1)
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.PREFIX
                                + plugin.getConfigFileManager().
                                getMsgConfigFile().getString("Track_Player_Dist") + tracker.getDistOfNearestWanted(player)));
                    else
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.PREFIX
                                + plugin.getConfigFileManager().
                                getMsgConfigFile().getString("Track_Again")));
                } else if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.PREFIX
                            + plugin.getConfigFileManager().
                            getMsgConfigFile().getString("Track_NoLonger")));
                    tracker.stopTracking(player);
                }
            }
    Whenever I am left clicking in spigot version 1.12, I get the error. While in 1.13 and 1.13+ I do not. Any reason why?
     
  2. You could just do a check and print to the console to see what is the actual null value here. If the item isn't null, it's going to be a null value somewhere in the second part of the AND expression.

    I'm betting it will be somewhere here:

    Code (Java):
    plugin.getConfigFileManager().getMsgConfigFile().getString("GTACops_Gui.MainPanel.Compass.Name")
     
  3. I used the same config in 1.13 and later version. It didn't pop out any error while left clicking. Only when I revert back to 1.12 I get this error

    Also plugin.getConfig()... Can't throw null because I have added "" at the end of the Line just to avoid null case
     
  4. drives_a_ford

    Junior Mod

    The javadocs of ItemMeta#getDisplayName clearly states:
    Check for ItemMeta#hasDisplayName before accessing the display name. It can be null. (i.e when setting it to null)
     
    • Like Like x 1
  5. Ye
    Yep solved the issue. I don't know why Spigot 1.13 and above didn't have such an issue whereas 1.12 had. Even though it was the same code and the same config file.
    Maybe 1.13 while doing getDisplayName() implicitly calls hasDisplay() function.
     
Thread Status:
Not open for further replies.