NullPointerException

Hi…

A NullPointerException is occurring at following place.

java.lang.NullPointerException
at com.abnamro.gddm.clm.bot.Discourse.processMessage(Discourse.java:230)

    at com.abnamro.gddm.clm.bot.MessageDispatcher.processPacket(MessageDispa

tcher.java:55)
at org.jivesoftware.smack.PacketReader$ListenerWrapper.notifyListener(Pa
cketReader.java:822)
at org.jivesoftware.smack.PacketReader.processListeners(PacketReader.jav
a:260)
at org.jivesoftware.smack.PacketReader.access$100(PacketReader.java:43)
at org.jivesoftware.smack.PacketReader$2.run(PacketReader.java:72)

It seems that smack API is passing null to the bot code because there is no possibility of entering a null value through the keyboard in the chat window. Can anyone suggest what could be the reason of this NullPointeException!!! Is there any place where this exception can be handled?

and the code is,


Discourse.java

public *void *processMessage( Message aMessage ) {

String myMessageBody = aMessage.getBody();

boolean wasProcessed = false;

wasUsed();

for (String myRegex : commands.keySet()) {

{color:#ff0000}if (myMessageBody.matches(myRegex)) { {color} //******** processMessage(Discourse.java:230)*******

processMessageUsingMethod( commands.get(myRegex), aMessage );

wasProcessed = true;

}

}

if (wasProcessed == false) {

try {

processDefaultHelpMessage( aMessage );

} catch(ClcmBaseException clcmBaseException) {

logger.fatal(clcmBaseException.getMessage());

sendMessage(“Exception Occured \n” +clcmBaseException.getMessage());

}

}

wasUsed();

}


MessageDispatcher.java

public void processPacket( Packet aPacket ) {

Message myMessage = (Message)aPacket;

Discourse myDiscourse = bot.getDiscourse( myMessage.getFrom() );

myDiscourse.processMessage( myMessage ); //MessageDispatcher.processPacket(MessageDispatcher.java:55)

}


**Smack code: **

PacketReader.java

/**

  • A wrapper class to associate a packet collector with a listener.

*/

private static class ListenerWrapper {

private PacketListener packetListener;

private PacketCollector packetCollector;

public ListenerWrapper(PacketReader packetReader, PacketListener packetListener,

PacketFilter packetFilter)

{

this.packetListener = packetListener;

this.packetCollector = new PacketCollector(packetReader, packetFilter);

}

public boolean equals(Object object) {

if (object == null) {

return false;

}

if (object instanceof ListenerWrapper) {

return ((ListenerWrapper)object).packetListener.equals(this.packetListener);

}

else if (object instanceof PacketListener) {

return object.equals(this.packetListener);

}

return false;

}

public boolean notifyListener() {

Packet packet = packetCollector.pollResult();

if (packet != null) {

{color:#ff0000}packetListener.processPacket(packet); //smack.PacketReader$ListenerWrapper.notifyListener(PacketReader.java:822)*

return true;

}

else {

return false;

}

}

public void cancel() {

packetCollector.cancel();

packetCollector = null;

packetListener = null;

}

}

**----


**

private void processListeners() {

while (!done) {

synchronized (listeners) {

if (listeners.size() > 0) {

for (int i=listeners.size()-1; i>=0; i–) {

if (listeners.get(i) == null) {

listeners.remove(i);

}

}

}

}

boolean processedPacket = false;

int size = listeners.size();

for (int i=0; i<size; i++) {

ListenerWrapper wrapper = (ListenerWrapper)listeners.get(i);

if (wrapper != null) {

{color:#ff0000}processedPacket = processedPacket || wrapper.notifyListener(); //PacketReader.processListeners(PacketReader.java:260)*

}

}

if (!processedPacket) {

try {

// Wait until more packets are ready to be processed.

synchronized (listenerThread) {

listenerThread.wait();

}

}

catch (InterruptedException ie) {

// Ignore.

}

}

}

}


Please edit your post and put the code in code tags (see the help).

String Message.getBody()  // Returns the body of the message, or null if the body has not been set.

So you can get null here if the client sent an empty message. You need to check if myMessageBody is null before invoking methods on it.

I thought of the same solution to null check before comparison… but how to handle if it is null…?

if not null, then fine… but my question is how to handle the exception?


(the user should safely be asked to retry!!!)

ofjeev wrote:

I thought of the same solution to null check before comparison… but how to handle if it is null…?

That is a descicions you make based on your requirements (for example, just forget that the empty message ever existed )

if not null, then fine… but my question is how to handle the exception?

surround it with try-catch statement

-----(the user should safely be asked to retry!!!)

Again, based on your requirements and design of the application. You could output a message in the receiving client, that an empty/useless message was retrieved, or generate a response that tells the sending end.

Eventhough the handling of a null value will fix the problem and prevent server crashing, it will not be a solution as such. Because the user cannot input a null value by any chance and this exception is occurring suddenly leading to server crashing. This problem is not replicable too and no steps to reproduce it.

“java.lang.NullPointerException at clm.bot.Discourse.processMessage(Discourse.java:230)”

the location is

String myMessageBody = aMessage.getBody(); // if the body has not been set, it will return null. But this setBody is present in org.jivesoftware.smack.packet.Message

if(myMessageBody.matches(myRegex)){ //Null pointer exception occurs here


}

Hence You mean that it is the problem of Smack which is actually at client side in various machines.

So is there any remedy for this by having any latest version of open fire.

As this is not replicable and this exception occurs suddenly, I do not have any way to test it also expect passing a null value manually in the code.

Can you please help me whether it could be solved by installing a latest version?

The only thing I can think of is that you are getting and trying to process packets that aren’t “chat” messages.

You need to configure your PacketFilter correctly, or check what type of Packet you got in processPacket().