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");
}
}
}
}