discord jda bot在调用扩展类时抛出nullpointerexception

bvuwiixz  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(519)

我目前正试图在jda中编写一个非常基本的discord bot,在试图将代码整齐地划分为一个主类时遇到了一个问题,即 MessageHandler ,一个 CommandHandler 和一个 VoiceHandler 班级。
如果接收到事件,则主类将添加eventlistener来调用messagehandler,如果在命令之前看到bot前缀,则将其分支到commandhandler。
问题是,当它尝试用调用commandhandler时抛出nullpointerexception new CommandHandler.handleCmd . 错误信息显示 "ERROR JDA - One of the EventListeners had an uncaught exception" 但如果我没有错的话,它应该只分支到commandhandler类的handlecmd函数。所以我不明白为什么会产生错误。
任何帮助都将不胜感激!
这是我的主课

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import javax.security.auth.login.LoginException;

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws LoginException {
        JDABuilder builder = new JDABuilder(AccountType.BOT);
        String token = "somethingsomething";
        builder.setToken(token);
        builder.addEventListeners(new MessageHandler());   //Puts Eventlistener to trigger MessageHandler

        builder.build();
    }
}

会触发我的消息处理程序

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class MessageHandler extends ListenerAdapter {
    protected String userName;
    protected String userMessage;
    protected MessageReceivedEvent event;

    public void onMessageReceived(MessageReceivedEvent event) {
        userName = event.getAuthor().getName();
        userMessage = event.getMessage().getContentRaw();
        this.event = event;

        if (event.getAuthor().isBot()) {
            return;
        } else if (userMessage.startsWith(">")) {
            new CommandHandler().handleCmd();                            //Should trigger CommandHandler (this is the problem)
        }
    }
}

然后分支到commandhandler

public class CommandHandler extends MessageHandler {
    private String command = userMessage.substring(1);

    public void handleCmd() {
        switch (command) {
            case "join":
                new VoiceHandler().join();
                break;
            case ... :
                ...
                break;
            case ... :
                ...
                break;
        }
    }
}

join()方法只会使bot连接到我的语音频道,但这现在应该无关紧要了
如果有帮助的话,这里还有完整的错误信息

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException
    at CommandHandler.<init>(CommandHandler.java:2)
    at MessageHandler.onMessageReceived(MessageHandler.java:17)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:376)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:64)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:151)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:122)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:853)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:741)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:720)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:891)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
nfeuvbwi

nfeuvbwi1#

stacktrace告诉您异常发生在哪一行(第2行) CommandHandler ).
你的领域 userMessagenull 您正在对它执行方法调用。如果 userMessage 尚未在commandhandler中设置。
下面的代码

userMessage = event.getMessage().getContentRaw();

仅在当前messagehandler对象中设置usermessage。这些值不会与新创建的commandhandler对象共享。您需要使用构造函数或方法参数显式地设置它们。也可以将这些字段更改为静态字段。
如何使用构造函数:

public class CommandHandler extends MessageHandler {
    private String command;

    /**
    *   You can add more parameters to this constructor or also a MessageHandler object.
  **/
    public CommandHandler(String userMessage){
        if(userMessage != null){
            this.comand = userMessage.substring(1);
        }
    }

    public void handleCmd() {
        switch (command) {
            case "join":
                new VoiceHandler().join();
                break;
            case ... :
                ...
                break;
            case ... :
                ...
                break;
        }
    }
}

对象创建如下所示:

if (event.getAuthor().isBot()) {
    return;
} else if (userMessage.startsWith(">")) {
    new CommandHandler(userMessage).handleCmd();
}

问题是为什么需要扩展messagehandler?您将其用作实用程序类,因此我建议将其更改为实用程序类:

public final class CommandHandler {

    public static void handleCmd(String userMessage) {
        if(userMessage == null){
            return;
        }
        String command = userMessage.substring(1);

        switch (command) {
            case "join":
                new VoiceHandler().join();
                break;
            case ... :
                ...
                break;
            case ... :
                ...
                break;
        }
    }
}

以及该方法的调用:

CommandHandler.handleCmd(userMessage);

相关问题