Essentials Hook isVanished() NullPointerException

Discussion in 'Plugin Help/Development/Requests' started by xDeeKay, Dec 17, 2014.

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

    xDeeKay

    Totally stuck here, I'm trying to run a check to see if a player is vanished using Essentials, but it's throwing a NullPointerException.

    Main:
    Code:
        private Essentials ess;
    
        public void onEnable() {
            ess = (Essentials) Bukkit.getServer().getPluginManager().getPlugin("Essentials");
          
            this.getServer().getPluginManager().registerEvents(new NotificationListener(this), this);
        }
    Listener:
    Code:
        @EventHandler
        public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
            for (Player notification : Bukkit.getOnlinePlayers()) {
                Player player = event.getPlayer();
                String playerName = player.getName();
                String world = player.getWorld().getName();
                if (!Main.toggleNotifications.contains(notification.getName())) {
                    if (!Main.deafen.contains(notification.getName())) {
                        if (!ess.getUser(player).isVanished()) {
                            if (world.toLowerCase().startsWith("guest")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.WHITE + world);
                            } else if (world.toLowerCase().contains("member")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.GRAY + world);
                            } else if (world.toLowerCase().contains("recruit")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.GOLD + world);
                            } else if (world.toLowerCase().contains("builder")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.GREEN + world);
                            } else if (world.toLowerCase().contains("crafter")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.DARK_PURPLE + world);
                            } else if (world.toLowerCase().contains("operator")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.AQUA + world);
                            } else if (world.toLowerCase().contains("admin")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.BLUE + world);
                            } else if (world.toLowerCase().contains("owner")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.RED + world);
                            } else if (world.toLowerCase().contains("lobby")) {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + ChatColor.BLUE + world);
                            } else {
                                notification.sendMessage(ChatColor.YELLOW + playerName + " changed map to " + world);
                            }
                        }
                    }
                }
            }
        }
    Error:
    Code:
    [19:33:09] [Server thread/ERROR]: Could not pass event PlayerChangedWorldEvent to Opticore vComedic Kangaroo
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.PlayerList.moveToWorld(PlayerList.java:561) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.PlayerList.changeDimension(PlayerList.java:622) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.EntityPlayer.c(EntityPlayer.java:511) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.Entity.K(Entity.java:263) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.EntityLiving.K(EntityLiving.java:154) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.Entity.s_(Entity.java:230) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.EntityLiving.s_(EntityLiving.java:1396) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.EntityHuman.s_(EntityHuman.java:173) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.EntityPlayer.l(EntityPlayer.java:267) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:353) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.PacketPlayInFlying.a(SourceFile:126) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.PacketPlayInPosition.a(SourceFile:57) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_25]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.NullPointerException
        at net.dkcraft.opticore.listeners.NotificationListener.onPlayerChangedWorld(NotificationListener.java:69) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:295) ~[craftbukkit-1.8-R0.1-SNAPSHOT.jar:git-Bukkit-7bc218d]
        ... 23 more
     
  2. Offline

    xize

    @xDeeKay

    What is line 69 from your event?
     
  3. Offline

    xDeeKay

    @xize Sorry, should have clarified that, it's line 9 in the listener.
     
  4. Offline

    xize

  5. Offline

    nverdier

    Well this is the error, as you stated. A NullPointerException is when you reference a variable that is empty. The only variable on that line is player, so the player variable is null.
     
  6. Offline

    SuperOriginal

    Make sure you have Essentials in the "depend" section of plugin.yml
     
  7. Offline

    Rocoty

    @nverdier Close but not quite. A NullPointerException occurs when you dereference a null pointer, or a null reference if you will. I.e a reference pointing to null. This may be a variable but is not limited as such. A method call (e.g getUser) can return a null pointer. If you dereference that (.isVisible) you end up with a NullPointerException.

    So to reiterate, the player variable may be null, but the error could also be that .getUser() returns null.
     
    Last edited: Dec 17, 2014
    mythbusterma likes this.
  8. Offline

    nverdier

    Dang it! Well thanks! Always thought it was a variable :p
     
  9. Offline

    xDeeKay

    @Rocoty So I need to check if the player exists like how @xize said?
     
  10. Offline

    mythbusterma

    @xDeeKay

    Simply add a null check before you use the Player, and after you call for the result from Essentials.
     
  11. Offline

    Rocoty

    @xDeeKay In this case, the ess variable may be a null reference, or getUser may return null. But the player variable isn't null due to the fact that the first stack trace element is on that line and you're not dereferencing player there.

    Please post the whole listener class, so we can help you verify if ess is null.

    EDIT: It would be fairly dumb if the player were null, to be fair. It is a player event after all.
     
  12. Offline

    xDeeKay

    Thanks guys, throwing in "if (essentials != null) {" did the trick!
     
  13. Offline

    Rocoty

    @xDeeKay That sounds like it would remove the error, but not fix your problem. Are you sure your plugin is doing what it's supposed to do now?
     
  14. Offline

    xDeeKay

    @Rocoty I also does what it's supposed to, have I overlooked something though?
     
  15. Offline

    Rocoty

    @xDeeKay I can't tell, as there is not enough context available for me to do so.
     
  16. Offline

    xDeeKay

    @Rocoty Then everything is fine, works the way I want with no errors, good enough for me!
     
    Rocoty likes this.
  17. Offline

    SuperOriginal

    The reason you probably needed that check is because your plugin loaded before essentials, which made ess null. As long as you add Essentials as a depend, should be fine.
     
Thread Status:
Not open for further replies.

Share This Page