Java - 调用 .buildConfiguration() 时出现 IllegalArgumentException
Java - IllegalArgumentException when .buildConfiguration() is called
因此,我为使用 PircBotX 制作的 IRC 机器人设置了一个 INI 文件,现在在 .buildConfiguration()
处抛出了 IllegalArgumentException
代码:
public Craftxbot() throws IOException{
File inicheck = new File("config.ini");
if(!inicheck.exists()){
System.out.println("Configuration file not found, Making one.");
inicheck.createNewFile();
Wini ini = new Wini(new File("config.ini"));
ini.add("Config");
System.out.print("Nickname: ");
ini.put("Config", "nick", input.next());
System.out.print("Server Hostname: ");
ini.put("Config", "serverHostname", input.next());
System.out.print("Server Port: ");
ini.put("Config", "serverPort", input.nextInt());
System.out.print("Default Channel: ");
ini.put("Config", "defaultChannel", input.next());
System.out.flush();
}
Wini ini = new Wini(new File("config.ini"));
System.out.print("Nickserv Password: ");
System.out.flush();
String nickpass = input.next();
Configuration<PircBotX> alpha = new Configuration.Builder<PircBotX>()
.setName(ini.get("Config", "nick"))
.setLogin("Craftxbot-Dev")
.setNickservPassword(nickpass)
.setAutoNickChange(true)
.setServerHostname(ini.get("Config", "serverHostname"))
.setServerPort(ini.get("Config", "serverPort", int.class))
.addAutoJoinChannel(ini.get("Config", "defaultChannel"))
.addListener(new Commands())
.addListener(new EventHandler())
.addListener(new PrivMsg())
.buildConfiguration();
堆栈跟踪:
Exception in thread "main" java.lang.IllegalArgumentException: Must specify name
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:93)
at org.pircbotx.Configuration.<init>(Configuration.java:128)
at org.pircbotx.Configuration$Builder.buildConfiguration(Configuration.java:589)
at tk.craftxbox.bot.Craftxbot.<init>(Craftxbot.java:61)
at tk.craftxbox.bot.Craftxbot.main(Craftxbot.java:27)
在添加 INI 文件之前,它工作正常并且符合预期
您的 ini.get 调用可能返回空值。原因之一是您在写下所有值后永远不会 ini.store()
。如果您查看此处的示例:http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html 您必须在写入值后调用 store()
。
因此,我为使用 PircBotX 制作的 IRC 机器人设置了一个 INI 文件,现在在 .buildConfiguration()
处抛出了 IllegalArgumentException代码:
public Craftxbot() throws IOException{
File inicheck = new File("config.ini");
if(!inicheck.exists()){
System.out.println("Configuration file not found, Making one.");
inicheck.createNewFile();
Wini ini = new Wini(new File("config.ini"));
ini.add("Config");
System.out.print("Nickname: ");
ini.put("Config", "nick", input.next());
System.out.print("Server Hostname: ");
ini.put("Config", "serverHostname", input.next());
System.out.print("Server Port: ");
ini.put("Config", "serverPort", input.nextInt());
System.out.print("Default Channel: ");
ini.put("Config", "defaultChannel", input.next());
System.out.flush();
}
Wini ini = new Wini(new File("config.ini"));
System.out.print("Nickserv Password: ");
System.out.flush();
String nickpass = input.next();
Configuration<PircBotX> alpha = new Configuration.Builder<PircBotX>()
.setName(ini.get("Config", "nick"))
.setLogin("Craftxbot-Dev")
.setNickservPassword(nickpass)
.setAutoNickChange(true)
.setServerHostname(ini.get("Config", "serverHostname"))
.setServerPort(ini.get("Config", "serverPort", int.class))
.addAutoJoinChannel(ini.get("Config", "defaultChannel"))
.addListener(new Commands())
.addListener(new EventHandler())
.addListener(new PrivMsg())
.buildConfiguration();
堆栈跟踪:
Exception in thread "main" java.lang.IllegalArgumentException: Must specify name
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:93)
at org.pircbotx.Configuration.<init>(Configuration.java:128)
at org.pircbotx.Configuration$Builder.buildConfiguration(Configuration.java:589)
at tk.craftxbox.bot.Craftxbot.<init>(Craftxbot.java:61)
at tk.craftxbox.bot.Craftxbot.main(Craftxbot.java:27)
在添加 INI 文件之前,它工作正常并且符合预期
您的 ini.get 调用可能返回空值。原因之一是您在写下所有值后永远不会 ini.store()
。如果您查看此处的示例:http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html 您必须在写入值后调用 store()
。