" is null 异常-6ren"> " is null 异常- 这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭去年。 我正在开发 JDA Discor-6ren">
gpt4 book ai didi

java.lang.NullPointerException : Cannot read the array length because "" is null 异常

转载 作者:行者123 更新时间:2023-12-05 04:54:28 30 4
gpt4 key购买 nike

<分区>

我正在开发 JDA Discord Bot,每次运行它时,我都会收到此异常。

java.lang.NullPointerException: Cannot read the array length because "<local3>" is null
at com.houseofkraft.handler.CommandHandler.scanIndex(CommandHandler.java:42)
at com.houseofkraft.core.DiscordBot.<init>(DiscordBot.java:68)
at com.houseofkraft.Stratos.main(Stratos.java:13)

我试图制作一个基本的命令处理程序,这里是它的代码:

 public void scanIndex(Index index) throws IOException, InvalidLevelException {
String[] commandList = index.indexClass;

for (String classPath : commandList) {
if (classPath.startsWith("com.houseofkraft")) {

String[] classPathSplit = classPath.split("\\.");
String commandName = classPathSplit[classPathSplit.length-1].toLowerCase();

commandPaths.put(commandName, classPath);
DiscordBot.logger.log("Added " + commandName + " / " + classPath + " to path.", Logger.DEBUG);
}
}
}

索引.java:

package com.houseofkraft.command;

public class Index {
public String[] indexClass;

public String[] getIndexClass() {
return indexClass;
}

public Index() {
String[] indexClass = {
"com.houseofkraft.command.Ping",
"com.houseofkraft.command.Test"
};
}
}

我不确定为什么会导致异常。谢谢!

编辑:这是我的 DiscordBot 代码

    public DiscordBot() throws IOException, ParseException, LoginException, InvalidLevelException {
try {
if ((boolean) config.get("writeLogToFile")) {
logger = new Logger(config.get("logFilePath").toString());
} else {
logger = new Logger();
}

logger.debug = debug;

info("Stratos V1");
info("Copyright (c) 2021 houseofkraft");

info("Indexing commands...");
// Add the Commands from the Index
commandHandler.scanIndex(new Index()); // here is the part that I call
info("Done.");

info("Connecting to Discord Instance...");
jda = JDABuilder.createDefault(config.get("token").toString()).addEventListeners(new EventHandler(commandHandler)).build();

if (jda != null) {
info("Connection Successful!");
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

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