NPE on plugin.getConfig()。set(...,...);

时间:2017-11-14 20:47:24

标签: java minecraft bukkit

我在我的Spigot插件上有一个错误,当我尝试执行/ samrandom true / false时返回空指针。这是我的代码: 主要课程:

package com.vandendaelen.simpleautomessage;

import java.io.File;
import java.util.List;
import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import com.vandendaelen.simpleautomessage.Commands.CommandSamRandom;
import com.vandendaelen.simpleautomessage.Commands.CommandSamTime;

public class SimpleAutoMessage extends JavaPlugin {
    public static final String RANDOM_CONFIG ="Random enabled";
    private int iMessages = 0;

    @Override
    public void onDisable() {
        // TODO Auto-generated method stub
        super.onDisable();
    }

    @Override
    public void onEnable() {
        System.out.println("Waw, an amazing plugin powered by LotuxPunk ! :-)");
        this.getCommand("samtime").setExecutor(new CommandSamTime(this));
        this.getCommand("samrandom").setExecutor(new CommandSamRandom(this, RANDOM_CONFIG));
        createConfig();

        this.getConfig().addDefault(RANDOM_CONFIG, false);
        this.getConfig().options().copyDefaults(true);
        saveConfig();

        //Enable display of messages
        if(getConfig().getBoolean("Enable")) {
            if(getConfig().getBoolean(RANDOM_CONFIG)) {
                messageRandomDisplayer();
            } else {
                messageDisplayer();
            }
        }


    }

    private void createConfig() {
        try {
            if (!getDataFolder().exists()) {
                getDataFolder().mkdirs();
            }
            File file = new File(getDataFolder(), "config.yml");
            if (!file.exists()) {
                getLogger().info("Config.yml not found, creating!");
                saveDefaultConfig();
            } else {
                getLogger().info("Config.yml found, loading!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    ...

}

命令类:

package com.vandendaelen.simpleautomessage.Commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class CommandSamRandom implements CommandExecutor {

    private String RANDOM_CONFIG;
    private Plugin plugin;

    public CommandSamRandom(Plugin pl, String r) {
        pl = plugin;
        RANDOM_CONFIG = r;
        //System.out.println(plugin.getConfig().getBoolean(RANDOM_CONFIG));
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player p = (Player)sender;
        if(args[0]!="") {
            Boolean randomEnabled = Boolean.parseBoolean(args[0]);
            if(p.hasPermission("simpleautomessage.setrandom")||p.isOp()) {
                plugin.getConfig().set(RANDOM_CONFIG, randomEnabled);

                if(randomEnabled) {
                    p.sendMessage("§2Random enabled");
                } else {
                    p.sendMessage("§4Random disabled");
                }
                plugin.saveConfig();
                plugin.reloadConfig();
                return true;
            }
        }

        return false;
    }

}

配置文件:

#Time between 2 messages (minutes)
Time: 15
#Auto-Messages !
Enable: true

#Broadcast randomly your messages
Random enabled: false

Messages:
  - "A simple string"
  - "Another string"
  - "Don't forget to support/rate LotuxPunk on Curse/Bukkit website !"

结果:

[20:56:05 INFO]: LotuxPunk issued server command: /samrandom true
[20:56:05 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'samrandom' in plugin SimpleAutoMessage v0.5
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
        ...
Caused by: java.lang.NullPointerException
        at com.vandendaelen.simpleautomessage.Commands.CommandSamRandom.onCommand(CommandSamRandom.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
        ... 15 more

有人可以帮我吗? :)

第26行是

plugin.getConfig().set(RANDOM_CONFIG, randomEnabled);

1 个答案:

答案 0 :(得分:2)

您的问题出在构造函数中:

private Plugin plugin;

public CommandSamRandom(Plugin pl, String r) {
    pl = plugin;
}

您将尚未初始化的字段plugin的值分配给反过来的参数pl insterad。 当您尝试访问实例字段以使其更明显时,有助于明确使用this.plugin