Skip to content Skip to sidebar Skip to footer

Nullpointerexception Thrown In Java Synchronised Method Using Sockets

I have an android app written in java. The app basically connects to a device which sends the app messages. The app waits for the messages to come in and then reports them, before

Solution 1:

My guess would be that an exception is thrown in processMessage, caught in the catch bellow it, but the getCause is null.

1/ Try to log the exception before handling it

2/ Look in processMessage, there should be an exception thrown.

Solution 2:

if((newResponse==null) || ("".equals(newResponse))

Or use brilliant guava's:

if(Strings.isNullOrEmpty(newResponse))

Solution 3:

message or newResponse may be null because it will get converted to message.toString() and newResponse.toString() by compiler.

Add condition for null check :

if(message != null && newResponse!=null) {
   newResponse = newResponse + message;
} else {
   // throw exception ...
}

Solution 4:

One thing which I see is that in reportMessage(), you are checking whether newResponse is an empty string - however you have not initialized it before, so it is most likely null. Probably what you want is

if(newResponse == null) {
    newResponse = newString();
}

Solution 5:

Instead of:

if("".equals(newResponse))

Use this:

if(newResponse==null)

An empty string is not the same as a null object; therefore, in cases when newResonse is null, it won't be initialized with a proper String object, and you wind up with the NPE.

Post a Comment for "Nullpointerexception Thrown In Java Synchronised Method Using Sockets"