1

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

END OF EDIT 1

public synchronized void reportMessage(String message)
{
    try
    {
        newResponse = newResponse + message;

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

When I run the application in debugger mode, it 'suspends' on the line:

newResponse = newResponse + message;

It says in the debug window:

Thread[<9> Thread-10] (Suspended (exception NullPointerException))

This occurs only some of the time. Sometimes it runs the line fine.

It never goes into the catch clause and when you click continue, the app crashes. There isn't a break point on the line so I don't even know why it is suspending there.

newResponse is of type String, defined as a global variable.

Can anyone help?

5
  • 2
    is newResponse is initialize at global level ?? Commented Jul 23, 2012 at 13:03
  • what is the value of newResponse and message when the application crashes? Commented Jul 23, 2012 at 13:04
  • are you creating string as "String newResponse" if yes, than you have to initialized to "String newResponse=''" . I hope it will work Commented Jul 23, 2012 at 13:06
  • Sorry @AndroidCoader, I forgot to add code that occurs earlier. I think what I have is sufficient to initialise it correctly. Commented Jul 23, 2012 at 13:21
  • @ftom2 The value of newResponse is "" and the value of message is ÌÌÌÎfffffs3333ÌÌÌÍÌÌÌÌæffs3333Æs33ÌÌͳ33æfs333̳33æ..... (1024 characters long) which looks weird but is what it is supposed to be. Commented Jul 23, 2012 at 13:23

4 Answers 4

5
try
    {
        // NOW add following condition and initialize newResponce only when it is null
        if(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
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, but that doesn't seem to help. I have added what you suggested but I think my initialisation that I have already was sufficient, see edit 1 in code.
See your question. I HAVE EDITED IT and try that then say.
I can't have newResponse initialised in that method as sometimes newResponse = "actual text" before the method is run. WHen I add System.out.println(newResponse); and System.out.println(message) in before newResponse = newResponse + message;, it is suspended on System.out.println(newResponse);
and some time you don't initialize newResponse with any text ?? Am I right? So that time you are getting NullPointerException.
yes, sometimes newResponse = "". But I want it just just = blank not equal null
|
2
public synchronized void reportMessage(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..

8 Comments

Now it is doing the same thing on the line: if(newResponse == null){
try if(newResponse.equals(null)){
if (newResponse == null) should not cause a NullPointerException. In fact, that is one of the very few legit ways to check whether a reference is null.
yes, that's what I think. Still we can check other way by using if(newResponse.equals(null))
Nope, still the same. And I tried putting "" instead of null in both cases as well.
|
0

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.

1 Comment

Neither appears to be null. newResponse = "" but that should be al-right shouldn't it?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.