I am working on a JDA Discord Bot and everytime I run it, I get this exception.
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)
I was attempting to make a basic Command Handler and here is the code for it:
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);
}
}
}
Index.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"
};
}
}
I'm not exactly sure why it causes the Exception. Thanks!
EDIT: Here is my DiscordBot Code
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();
}
}
}