抛出String的java/android NullPointerException

Pip*_*ith 1 java string debugging android nullpointerexception

我有一个看似非常简单的方法,用java编写的android应用程序:

编辑1:私有字符串newResponse;

public SOME METHOD CALLED FIRST
{
    newResponse = "";
}
Run Code Online (Sandbox Code Playgroud)

编辑结束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();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在调试器模式下运行应用程序时,它'挂起'在该行上:

newResponse = newResponse + message;
Run Code Online (Sandbox Code Playgroud)

它在调试窗口中说:

线程[<9>线程-10](暂停(异常NullPointerException))

这种情况仅在某些时候发生.有时它会很好地运行.

它永远不会进入catch子句,当您单击继续时,应用程序崩溃.线路上没有断点,所以我甚至不知道为什么它会暂停.

newResponse的类型为String,定义为全局变量.

有人可以帮忙吗?

Chi*_*ani 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
    }
Run Code Online (Sandbox Code Playgroud)