0

How do you fix the following error in Bukkit?

public class Listeners implements Listener {

private Main plugin = Main.getPlugin(Main.class);
@SuppressWarnings("deprecation")
public  void sendWarnTitle(Player player){
    player.sendTitle(ChatColor.GRAY + "Ты получишь ничего, если простоишь на точке "+ ChatColor.YELLOW + plugin.getConfig().getInt("time") + "секунд." ,ChatColor.GRAY + "Если сойдёшь с кнопки, то время начнётся заного");
    new BukkitRunnable(){
        @Override
        public void run(){
            plugin.time.put(player,plugin.getConfig().getInt("time")); // This is line 37, where the exception is thrown
            this.cancel();
        }
    }.runTaskTimer(plugin,0,60);
}

public void countdown(Player player){
    new BukkitRunnable(){
        @SuppressWarnings("deprecation")
        @Override
        public void run() {
            if (plugin.time.get(player) > 0) {
                plugin.time.put(player, plugin.time.get(player) - 1);
                player.sendTitle("",ChatColor.GRAY + "Точка будет захвачена через " + ChatColor.YELLOW + plugin.time.get(player) + "секунд.");
            }
            if(plugin.time.get(player)== 0){
                plugin.time.remove(player);
                this.cancel();
            }
        }
    }.runTaskTimer(plugin, 20 ,20);
}

LOGS:

[Server thread/WARN]: [CapturePlate] Task #3 for CapturePlate v0.1 generated an exception
java.lang.NullPointerException: null
    at main.main.Listeners$2.run(Listeners.java:37) ~[?:?]
    at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:361) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    at java.lang.Thread.run(Thread.java:813) [?:1.8.0_212]
[12:28:24] [Server thread/WARN]: [CapturePlate] Task #5 for CapturePlate v0.1 generated an exception
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Exp1
  • 1

1 Answers1

1

On line 37, assuming that time is a static HashMap in your main, you are likely trying to use get but the element does not exist, you should perform a contains check first:

if(!plugin.time.containsKey(player))
    return;
if (plugin.time.get(player) > 0) { 
    //etc

There are a few other non-functional issues but I'd like to stress against the use of @SuppressWarnings("deprecation") because it only serves to make updating your plugin harder. I assume you're doing it because sendTitle is deprecated, but if you read the docs on sendTitle you will see that it was deprecated because API behavior subject to change so it benefits you to not suppress the warning.

Lucan
  • 2,907
  • 2
  • 16
  • 30