gpt4 book ai didi

java - NPE on plugin.getConfig().set(... , ...);

转载 作者:行者123 更新时间:2023-12-02 02:29:53 28 4
gpt4 key购买 nike

我的 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);

最佳答案

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

private Plugin plugin;

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

您将尚未初始化的字段plugin的值分配给参数pl,而不是相反。当您尝试访问实例字段时,显式使用 this.plugin 会有所帮助,以使这一点更加明显。

关于java - NPE on plugin.getConfig().set(... , ...);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47294896/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com