IllegalArgumentException,插件不能为null

时间:2019-01-23 17:53:13

标签: java minecraft bukkit

插件的主类在其他类的顶部初始化时返回null:

java.lang.IllegalArgumentException: Plugin cannot be null

BeforeGame类中的初始化:

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

在主班:

公共类Main扩展JavaPlugin实现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;
}

} 部分代码出现错误:

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);
}

控制台告诉您,问题出在具有实例的java:39上,是:

}.runTaskLater(instance, 50);

错误是:

[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]

1 个答案:

答案 0 :(得分:0)

在onEnable()方法中定义实例以及您将在构造函数中执行的其他所有操作。 之所以这样做,是因为Bukkit会清除变量,但是由于Java的ClassLoader的限制,每次重新加载服务器时Bukkit都不会创建主类的新实例。