Skip to content Skip to sidebar Skip to footer

Java / Android Nullpointerexception Thrown On A String

I have what seems like a very simple method, written in java for an android application: EDIT 1: private String newResponse; public SOME METHOD CALLED FIRST { newResponse

Solution 1:

try
    {
        // NOW add following condition and initialize newResponce only when it is nullif(null == newResponse)
        {
            newResponse = new String();
        }
        System.out.println("newResponse"+newResponse);  //<--Add this two lines
        System.out.println("message"+message); // and check which line gives you NullPointerException

        newResponse = newResponse + message;

        confirmQE(); //Look for qe in the message
    }

Solution 2:

public synchronized voidreportMessage(String message)
{
    try
    {
        if(newResponse == null){
            newResponse = message;
        }else{
            newResponse = newResponse + message;
        }

        confirmQE(); //Look for qe in the message
    }
    catch (Exception e)
    {
        response = e.getCause().toString();
    }
}

Try above code..

Solution 3:

Inspect the individual variables to see which one is null.

Also, e.getCause() may be returning null also, so you may have an exception inside your exception handler as well.

Solution 4:

I have fixed the problem.

For anyone wondering, I added

if("".equals(newResponse))
{ 
    newResponse = new String();
}

before

newResponse = newResponse + message;

And that prevents the error.

Thanks for everyone's help.

Post a Comment for "Java / Android Nullpointerexception Thrown On A String"