Nullpointer asyncPlayerChatEvent

Discussion in 'Spigot Plugin Development' started by DaanSander, May 29, 2015.

  1. Hello I was just uploading my plugin from my local computer to my hosted server but some things wont work on my hosted server whats on my local server works just fine i have the java jdk 7 and the same spigot version in the build path can someone help me

    Method that isnt working:
    Code (Text):
    @EventHandler(priority = EventPriority.MONITOR)
        public void onPlayerRankChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String admin = "§c§lAdmin ";
            String builder = "§6§lBuilder";
            String Normal = "";
            String Dev = "§4§lDeveloper";
            String HMC = "§a§lHMC";
            String HMCP = "§a§lHMC+";
            String MOD = "§6§lMOD";
            String Helper = "§1§lHelper";
            String SRMOD = "§6§lSR.MOD";
            String message = e.getMessage();
            if (!(message.equals(message.toUpperCase()))) {
                goodm = message.substring(0, 1).toUpperCase() + message.substring(1);
            } else {
                p.sendMessage(ChatColor.RED + "Message cannot be uppercase!");
                message = message.toLowerCase();
                goodm = message.substring(0, 1).toUpperCase() + message.substring(1);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("default")) {
                e.setFormat(Normal + "" + p.getName() + "§8 " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("builder")) {
                e.setFormat(builder + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("admin")) {
                e.setFormat(admin + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("Helper")) {
                e.setFormat(Helper + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("HMC")) {
                e.setFormat(HMC + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("HMCP")) {
                e.setFormat(HMCP + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("Admin")) {
                e.setFormat(admin + "§r§8" + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("Mod")) {
                e.setFormat(Normal + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("Developer")) {
                e.setFormat(Dev + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("SR.Mod")) {
                e.setFormat(SRMOD + "§r§8 " + p.getName() + "§r " + goodm);
            }
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("Mod")) {
                e.setFormat(MOD + "§r§8 " + p.getName() + "§r " + goodm);
            }
    Yes i know the code is a bit messy but i was going to clean it after this

    Stacktrace:
    Code (Text):
    [17:48:50] [Async Chat Thread - #0/ERROR]: Could not pass event AsyncPlayerChatEvent to HMCEssentials v2.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PlayerConnection.chat(PlayerConnection.java:1055) [spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:993) [spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_55]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_55]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_55]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_55]
        at java.lang.Thread.run(Thread.java:744) [?:1.7.0_55]
    Caused by: java.lang.NullPointerException
        at me.daansander.hmcessentials.playerlisteners.PlayerChat.onPlayerRankChat(PlayerChat.java:40) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_55]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_55]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_55]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_55]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.3.jar:git-Spigot-ea179b3-6e0120a]
        ... 11 more
    sorry for bad english

    EDIT:
    I kow its a nullpointer but i cant find whats null
     
  2. What is PlayerChat.java line 40?
     
  3. Code (Text):
            if (HMCEssentials.getPlugin().playerRanks.get(p).equalsIgnoreCase("default")) {
     
  4. playerRanks probably has no p
     
  5. It is beening set in the start of the server
    Code (Text):
    Bukkit.getScheduler().runTaskLater(this, new Runnable() {
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        if (p.hasPermission("HMCEssentials.adminmessage")) {
                            p.sendMessage(ChatColor.GREEN + "Starting ranks reload...");
                        }
                        HMCEssentials.getPlugin().playerRanks.put(p, HMCEssentials.getPlugin().getSql().getRank(p));
                        if (p.hasPermission("HMCEssentials.adminmessage")) {
                            p.sendMessage(ChatColor.GREEN + "Ranks reload complete!");
                        }
                    }
                }
            }, 80L);
    While i am just online
     
  6. Checking if it's there won't hurt you...
    Maybe getPlugin() is null or playerRanks is null
     
  7. plugin cant be null it is set in the onEnable():
    plugin = this;
    But how can it be null on my offline server it works just fine
     
  8.  
  9. @DaanSander
    • You shouldn't abuse static for accessing the data in the Map. Use a constructor to pass the plugin instance.
    • You shouldn't be accessing the Map directly, create a method which takes a player and returns theMap.get(player).
    • Also, you can just get the data once, assign it to a variable, and then reuse that variable each check.
     
    • Agree Agree x 1