6

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();
        }
    }
}
houseofkraft
  • 191
  • 1
  • 2
  • 8

1 Answers1

2

You have a member variable public String[] indexClass in your Index class. In your constructor you create a new variable with

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

This way your member variable stays uninitialized. Change the code in the constructor to

this.indexClass = {
        "com.houseofkraft.command.Ping",
        "com.houseofkraft.command.Test"
};

BTW, the member variable should be private, not public, since you want to access it by getter (and than do access it by the getter in the CommandHandler).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
juwil
  • 422
  • 3
  • 9