【问题标题】:java.lang.NullPointerException: Cannot read the array length because "<local3>" is null [duplicate]java.lang.NullPointerException:无法读取数组长度,因为“<local3>”为空 [重复]
【发布时间】:2021-04-20 13:12:37
【问题描述】:

我正在开发一个 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();
        }
    }
}

【问题讨论】:

  • 请发布您的 DiscordBot 代码 - 至少是您调用 CommandHandler#scanIndex 的部分。参数索引的值是多少?
  • 我刚刚编辑了我的帖子以包含 DiscordBot 代码。

标签: java nullpointerexception discord-jda


【解决方案1】:

您的Index 类中有一个成员变量public String[] indexClass。在您的构造函数中,您使用

创建一个新变量
String[] indexClass = {
        "com.houseofkraft.command.Ping",
        "com.houseofkraft.command.Test"
};

这样你的成员变量保持未初始化。将构造函数中的代码改为

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

顺便说一句,成员变量应该是私有的,而不是公共的,因为您想通过 getter 访问它(而不是通过 CommandHandler 中的 getter 访问它)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2022-01-10
    • 2023-02-26
    • 2022-12-10
    • 2021-11-10
    相关资源
    最近更新 更多