Java - 调用.buildConfiguration()时出现IllegalArgumentException

时间:2016-02-19 02:31:56

标签: java ini4j

因此,我为使用PircBotX制作的IRC机器人设置了一个INI文件,现在抛出了一个IllegalArgumentException .buildConfiguration()

代码:

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文件之前,它可以正常工作

1 个答案:

答案 0 :(得分:0)

你的ini.get调用可能会返回null。这样做的一个原因是,在写完所有值后,您永远不会ini.store()。如果您查看此处的示例:http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html,则必须在写入值后调用store()