我有一个看起来很简单的方法,用java编写的android应用程序:

编辑1:
    私有String newResponse;

public SOME METHOD CALLED FIRST
{
    newResponse = "";
}


编辑结束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();
    }
}


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

newResponse = newResponse + message;


它在调试窗口中说:

线程[线程-10](已暂停(NullPointerException异常))

这仅在某些时间发生。有时它运行良好。

它永远不会进入catch子句,并且当您单击继续时,应用程序崩溃。生产线上没有断点,所以我什至不知道为什么它挂在那里。

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

有人可以帮忙吗?

最佳答案

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
    }

10-08 07:34