java/android NullPointerException 在字符串上抛出

标签 java android string debugging nullpointerexception

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

编辑 1: 私有(private)字符串 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;

它在调试窗口中说:

Thread[<9> Thread-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
    }

关于java/android NullPointerException 在字符串上抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11613060/

相关文章:

android - 如何在 Matlab 中生成 wav 文件的频谱图,频率在 x 轴上

java - 循环以随机显示数组中的图像(Android)

javascript - 如何将人类时间字符串转换为毫秒?

c - 将字符串的第一个字节存储到另一个字符串中 : algorithm of LZW Compression

java - Perl 和 Java 之间通过套接字发送字节数组

java - Java Lambdas线程安全

java - 从 javax.json.JsonArray 转换为 org.json.JSONArray

java - Sling 资源与节点

android - 在应用程序结果中添加UsbSerialForAndroid库导致Gradle错误

c - 如何使用strchr在c中获取子字符串