discord-jda / JDA

Java wrapper for the popular chat & VOIP service: Discord https://discord.com
Apache License 2.0
4.34k stars 735 forks source link

[Fatal] [JDA]: One of the EventListeners had an uncaught exception #114

Closed ubercd closed 8 years ago

ubercd commented 8 years ago

Cannot get my simple ping bot to work~ getting the following errors

[21:55:03] [Fatal] [JDA]: One of the EventListeners had an uncaught exception [21:55:03] [Fatal] [JDA]: Encountered an exception: java.lang.NullPointerException at Commands.PingCommand.action(PingCommand.java:19) at Bot1.handleCommand(Bot1.java:41) at BotListener.onMessageReceived(BotListener.java:13) at net.dv8tion.jda.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:176) at net.dv8tion.jda.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:57) at net.dv8tion.jda.handle.MessageReceivedHandler.handleInternally(MessageReceivedHandler.java:65) at net.dv8tion.jda.handle.SocketHandler.handle(SocketHandler.java:36) at net.dv8tion.jda.requests.WebSocketClient.handleEvent(WebSocketClient.java:545) at net.dv8tion.jda.requests.WebSocketClient.onTextMessage(WebSocketClient.java:296) at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352) at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:233) at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:211) at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:910) at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:693) at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:102) at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:61)

jagrosh commented 8 years ago

The null pointer exception is in your own code. Can you upload that class to hastebin? I don't think this is a JDA issue.

DV8FromTheWorld commented 8 years ago

Yeah, this doesn't look to be a JDA issue. provide the PingCommand class on http://hastebin.com please.

ubercd commented 8 years ago

http://hastebin.com/uhotuxawom.java

jagrosh commented 8 years ago

In the code you shared, line 19 is blank

Also, MessageReceivedEvent#getTextChannel() can be null if the message was a Direct/Private message

ubercd commented 8 years ago

sorry, re-ran it and now it's saying line 16 rather than 19

jagrosh commented 8 years ago

That will throw a NPE whenever the bot gets a private message

ubercd commented 8 years ago

@jagrosh what's the fix then? im just trying to get it to respond with anything obviously

jagrosh commented 8 years ago

If you take a look at the docs, you'll see the TextChannel extends Channel, which represents a Guild channel. MessageReceivedEvent is for both Guild and Private messages. You need to either use the MessageReceivedEvent#getPrivate() boolean to determine if you need getTextChannel() or getPrivateChannel(), or use the MessageChannel interface returned from MessageReceivedEvent#getChannel()

ubercd commented 8 years ago

thanks. the bot responds correctly when pinged from the server but not private messages.

jagrosh commented 8 years ago

Considering that this was an error in your own code, would you mind closing this issue?

jagrosh commented 8 years ago

Also, if you have more questions, this channel will be an excellent resource: https://discord.gg/0SBTUU1wZTUgGuC8