Hello! I have this small problem, i get this error code but i can't fix it. Code: [22:01:52 WARN]: [ServerMod] Plugin ServerMod v1.0 generated an exception while executing task 55 java.lang.NullPointerException at me.bigflori.mod.events.ChatEvent$1.run(ChatEvent.java:69) ~[?:?] at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot112.jar:git-Spigot-625bc00-f4822eb] at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:52) [spigot112.jar:git-Spigot-625bc00-f4822eb] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_131] at java.lang.Thread.run(Unknown Source) [?:1.8.0_131] And here is my code... Code: package me.bigflori.mod.events; import java.util.HashMap; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.scheduler.BukkitRunnable; import me.bigflori.mod.core; import net.md_5.bungee.api.ChatColor; public class ChatEvent implements Listener{ public static HashMap<String, Integer> cl = new HashMap<>(); private core cfg; public ChatEvent(core pl) { cfg = pl; } //COLOR CODES @EventHandler public void chat(AsyncPlayerChatEvent event) { if(me.bigflori.mod.core.colorcodes == true) { String msg = ChatColor.translateAlternateColorCodes('&', event.getMessage()); event.setMessage(msg); } } //NICKNAME @EventHandler public void chat2(AsyncPlayerChatEvent event) { Player p = event.getPlayer(); String uuid = p.getUniqueId().toString(); if(cfg.getConfig().contains(uuid)) { String dsp = cfg.getConfig().getString(uuid + ".nickname"); p.setDisplayName(dsp); } } //USING CHAT @EventHandler public void chat3(AsyncPlayerChatEvent event) { if(core.chat == false) { event.setCancelled(true); event.getPlayer().sendMessage("§cA chat használata jelenleg le van tiltva."); } } //ANTI FLOOD @EventHandler public void chat4(AsyncPlayerChatEvent event) { Player p = event.getPlayer(); if(!cl.containsKey(p.getName())) { cl.put(p.getName(), 2); new BukkitRunnable() { public void run() { if(cl.get(p.getName()) <= 0) { cl.remove(p.getName()); } cl.put(p.getName(), cl.get(p.getName()) - 1); } }.runTaskTimerAsynchronously(cfg, 1, 20); } else { event.setCancelled(true); p.sendMessage("Nem küldhetsz ilyen gyorsan üzenetet!"); } } }
@bigflori 1. Only 1 even handler per event please. 2. me.bigflori.mod.core.colorcodes, where does this come from? What is the value? 3. Remove all public static from your code please, you never need it in Bukkit.
Okay, I change my structure so this is my new class with the same code Code: package me.bigflori.mod.events; import java.util.HashMap; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.scheduler.BukkitRunnable; import me.bigflori.mod.core; public class ChatAntiflood implements Listener { private core cfg; public ChatAntiflood(core pl) { cfg = pl; } HashMap<String, Integer> cl = new HashMap<>(); //ANTI FLOOD @EventHandler public void chat4(AsyncPlayerChatEvent event) { Player p = event.getPlayer(); if(!cl.containsKey(p.getName())) { cl.put(p.getName(), 2); new BukkitRunnable() { public void run() { if(cl.get(p.getName()) == 0) { cl.remove(p.getName()); } cl.put(p.getName(), cl.get(p.getName()) - 1); } }.runTaskTimerAsynchronously(cfg, 1, 20); } else { event.setCancelled(true); p.sendMessage("Nem küldhetsz ilyen gyorsan üzenetet!"); } } } But I'm still get this error Code: [23:06:26 WARN]: [ServerMod] Plugin ServerMod v1.0 generated an exception while executing task 15 java.lang.NullPointerException at me.bigflori.mod.events.ChatAntiflood$1.run(ChatAntiflood.java:32) ~[?:?] at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot112.jar:git-Spigot-625bc00-f4822eb] at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:52) [spigot112.jar:git-Spigot-625bc00-f4822eb] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_131] at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
@bigflori The fact that you named the async method "chat4" is concerning. You should only have one method for event type for the whole class. If you have other asyncchatevent methods, merge them. Something is null on line 32. Can you post that line? Also, don't assume that the name will still be in the hashmap. Check and make sure that cl contains the player's name before getting it.
@bigflori Actually, line 36 is what's causing the NPE in the first place. Code: if(cl.get(p.getName()) == 0) { cl.remove(p.getName()); } cl.put(p.getName(), cl.get(p.getName()) - 1); // this line You're retrieving "p.getName()" from the hashmap after you removed it in the "if" block. But the task keeps running, and blames the NPE on line 32. Try putting the line in an else block, and cancel the event and return from the if block.