0

I have been trying Java Discord API for a while, and I tried to combine it with JavaFX. My program only has to log received messages in a TextArea, but every time I receive one, it crashes.

JDA throws:

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException
    at com.pequinho.discordmessagelogger.PrimaryController.setAreaText(PrimaryController.java:48)
    at com.pequinho.discordmessagelogger.PrimaryController.onMessageReceived(PrimaryController.java:70)
    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)

Also, I tested it with a simple sout, and It worked. It also works with the startLogging() method, which is linked to a Button. Note that the onMessageReceived() uses the same method, and it throws NullPointerException anyways:

public class PrimaryController extends ListenerAdapter {

    String line;
    boolean activeLog = true;
    ArrayList<String> history = new ArrayList<>();

    @FXML
    Button startLoggingBtn;

    @FXML
    TextArea loggingTextArea;

    public static void discordInit() throws LoginException {
        String discordToken = "??????????????";
        JDA jda = new JDABuilder(AccountType.CLIENT).addEventListeners(new PrimaryController()).setToken(discordToken).build();

    }

    public void setAreaText(String text) { loggingTextArea.setText(text); }
    public void appendAreaText(String text) { loggingTextArea.appendText(text); }

    @FXML
    private void startLogging() throws IOException {
        // setAreaText("Discord Message Logger started");
        this.history.clear();
        this.history.add(init);
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        if (!event.isFromType(ChannelType.TEXT)) {
            line = "Message from " + event.getAuthor().toString() + ": " + event.getMessage().getContentDisplay();

            this.history.add(line);

            loggingTextArea().clear;

            for(int i = 0;i < history.size();i++) {
                appendAreaText(history.get(i) + "\n");
            }
        }
    }
}
Peco
  • 1
  • 1
  • `loggingTextArea` is null. – James_D Apr 14 '20 at 19:34
  • @James_D All loggingTextArea methods throw `NullPointerException`, even if I use `setText()` method before. Same things work on `startLogging()`... – Peco Apr 14 '20 at 19:41
  • 1
    `.addEventListeners(new PrimaryController())` ← this adds an instance of `PrimaryController` as a listener but that instance is not related to any loaded FXML file, meaning none of the FXML-related fields are injected, thus your NPE. You would need to get the `PrimaryController` instance associated with the `FXMLLoader` and then use that instance as a listener with the JDA builder. – Slaw Apr 14 '20 at 19:45
  • 1
    This code cannot produce the stacktrace you posted. There's no `setAreaText` call in `onMessageReceived`, but according to the stacktrace this is what's happening. Furthermore you're pretty much guaranteed to run into trouble at some time, either since you're modifying teh gui from a background thread or since you're running a long-running operation on the application thread... – fabian Apr 14 '20 at 21:50

0 Answers0