-1

Plugin's main class returns null when I initialized it in top of other class:

java.lang.IllegalArgumentException: Plugin cannot be null

Initialization in BeforeGame class:

    Main instance;
public BeforeGame(Main instance) {
    this.instance = instance;
}

In main class:

public class Main extends JavaPlugin implements Listener {

private Main instance;
public Main getInstance(){
    return instance;
}

@Override
public void onEnable() {
    instance = this;
    Bukkit.getServer().getPluginManager().registerEvents(new LobbyHandlers(this), this);
    Bukkit.getServer().getPluginManager().registerEvents(new GameListener(this), this);
    BeforeGame bfGame = new BeforeGame(this);
    LobbyCountdown lobbyCountdown = new LobbyCountdown(this);
}

@Override
public void onDisable() {
    instance = null;
}

} Part of code with error:

public void loadWaiter(World w){
    List<Player> players = w.getPlayers();
    BukkitTask waitplz = new BukkitRunnable(){
        @Override
        public void run() {
            for(Player p : players){
                p.sendTitle(ChatColor.YELLOW + "Wait", ChatColor.YELLOW + "loading other players!", 0, 40, 10);
                p.getInventory().clear();
                blocksSet(players);

            }
        }
    }.runTaskLater(instance, 50);
}

Console tells, that problem is on java:39 with instance, it is:

}.runTaskLater(instance, 50);

Error is:

[23:38:05 WARN]: [BridgeBattle_Reworked] Task #36 for 
BridgeBattle_Reworked v0.1 generated an exception 
java.lang.IllegalArgumentException: Plugin cannot be null at 
org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot. 
jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.validate(Cra 
ftScheduler.java:410) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.runTaskTimer 
(CraftScheduler.java:130) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.runTaskLater 
(CraftScheduler.java:113) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.scheduler.BukkitRunnable.runTaskLater(BukkitRunnable.java: 75) 
~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
ru.vladis.bridgebattle.game.BeforeGame.loadWaiter(BeforeGame.java:39) ~ 
[?:?] at 
ru.vladis.bridgebattle.countdowns.LobbyCountdown$1.run(LobbyCountdown 
.java:51) ~[?:?] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.jav 
a:76) ~[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHe 
artbeat(CraftScheduler.java:361) [spigot.jar:git-Spigot-dcd1643-e60fc34] 
at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java: 
739) [spigot.jar:git-Spigot-dcd1643-e60fc34] at 
net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java: 406) 
[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java: 679) 
[spigot.jar:git-Spigot-dcd1643-e60fc34] at 
net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.jav 
a:577) [spigot.jar:git-Spigot-dcd1643-e60fc34] at 
java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
vladis086
  • 41
  • 1
  • 6

1 Answers1

0

Define the instance and everything else you would do in the constructor in the onEnable() method. The reason for doing so is because Bukkit clears the variables, but does not create a new instance of your main class every time your reload your server due to the limitations of the ClassLoader by java.

AUser
  • 105
  • 1
  • 7