【问题标题】:Why am I getting a nullpointer exception while I'm checking whether an array string is empty?为什么在检查数组字符串是否为空时出现空指针异常?
【发布时间】:2012-11-16 21:37:28
【问题描述】:

为什么在下面的第二个 if 语句中出现空指针错误?

如果数组字符串scores 是否为空,我正在检查该 if 语句,但它总是崩溃说空指针异常。如果有帮助,我还添加了 LogCat。

String[] scores;
String times = "";

public void Read() {
    FileInputStream inputStream = null;

    try {

        inputStream = openFileInput("timesfile");

        byte[] reader = new byte[inputStream.available()];

        while (inputStream.read(reader) != -1) {
        }
        times = new String(reader);

    } catch (IOException e) {
        e.printStackTrace();

    } finally {
        if (inputStream != null) {
            try {
                    inputStream.close();
            } catch (IOException e) {
                    e.printStackTrace();
            }

        }

    }

    if (!times.isEmpty() && times != null && times != ""
                    && times.length() != 0) 
    {
        scores = new String(times).split(",");
        Arrays.sort(scores);
    }
    if (scores.length != 0 && scores != null && scores.length >= 4) { //I get a nullpointer exception here!

        better = false;
        if (Integer.parseInt(endTime.split(":")[0]) > Integer
                        .parseInt(scores[0].split(":")[0])) {
            better = true;
        } else if (Integer.parseInt(endTime.split(":")[1]) > Integer
                        .parseInt(scores[0].split(":")[1])) {
            better = true;
        } else if (Integer.parseInt(endTime.split(":")[2]) > Integer
                        .parseInt(scores[0].split(":")[2])) {
            better = true;
        } else {
            better = false;
        }

    }

}

LogCat:

11-16 22:32:21.195: E/AndroidRuntime(28584): FATAL EXCEPTION: Thread-2278
11-16 22:32:21.195: E/AndroidRuntime(28584): java.lang.NullPointerException
11-16 22:32:21.195: E/AndroidRuntime(28584):    at com.example.app.TestActivity$TestView.scoreRead(TestActivity.java:426)
11-16 22:32:21.195: E/AndroidRuntime(28584):    at com.example.app.TestActivity$TestView.over(TestActivity.java:303)
11-16 22:32:21.195: E/AndroidRuntime(28584):    at com.example.app.TestActivity$TestView.run(TestActivity.java:267)
11-16 22:32:21.195: E/AndroidRuntime(28584):    at java.lang.Thread.run(Thread.java:856)

【问题讨论】:

  • 尝试将订单切换到if (times != null && !times.isEmpty() && times != ""
  • 这是第 426 行: if (scores.length != 0 && scores != null && scores.length >= 4) {
  • 总是在对象的任何属性之前检查空对象
  • 你可以只使用 if(!TextUtils.isEmpty(times)) 代替

标签: java android arrays string nullpointerexception


【解决方案1】:

您可以尝试将times != null 保留在最左侧。操作顺序是从左到右。

if (times != null && !times.isEmpty() && times != "" && times.length() != 0)

编辑: 是的,@Simon 是对的。times != "" 应该是 !times.equal("")

但无论如何你已经在检查!times.isEmpty(),所以你根本不需要那块。

应该这样做:

if (times != null && !times.isEmpty() )

【讨论】:

    【解决方案2】:

    先做if(times!= null)

    然后嵌套在那做!times.isEmpty()

    isEmpty() 检查字符串的长度是否为零。不为空或不为空。 0的长度是"",不为空

    如果是另一个被检查的字符串,同样的原则也适用

    【讨论】:

    • ^ 这个。您可以只使用 if(!TextUtils.isEmpty(times)) 代替
    【解决方案3】:

    替换

    if (scores.length != 0 && scores != null && scores.length >= 4) {
    

    if (scores != null && scores.length >= 4) {
    

    替换

    if (!times.isEmpty() && times != null && times != "" && times.length() != 0) 
    

    if (times != null && !times.isEmpty()) 
    

    即在条件前面放置空检查并删除不必要的防护。

    【讨论】:

      【解决方案4】:

      这不是安全检查。

      times != ""
      

      String 的相等运算符测试这 2 个 String 是否是同一个对象。相反,使用

      !times.equal("")
      

      != 有时可能会起作用,因为 Java “实习生”字符串是 ""

      【讨论】:

        【解决方案5】:

        您的问题和您的评论不匹配,但无论您检查的是 scores 还是 times,您必须检查它们是否是 nullfirst时间>。 (否则你会看到 NPE。)

        用途:

        if (times != null && !times.isEmpty() && times != "" && times.length() != 0)
        

        还有:

        if (scores != null && scores.length != 0 && scores.length >= 4) {
        

        为什么在下面的第二个 if 语句中出现空指针错误?

        当您尝试对变量 null 调用方法时,会发生 NullPointerException。因此,如果scoresnull,则此代码scores.length 正在尝试调用null.doSomething()。你不能要求什么都不做。

        【讨论】:

          【解决方案6】:

          实际上你的 score 数组只有在以下情况下才会被初始化

          !times.isEmpty() && times != null && times != "" && times.length() != 0

          是真的。它发生了,所以它不是真的:)。并且在时间上调用方法并将其比较为空是没有意义的。但是这里没有 NPE,所以我们忽略它。

          !times.isEmpty 等同于 times.legth != 0;

          times != "" 只要用 'new' 操作初始化并且没有被实习,就总是为真。

          所以所有条件都等于(你可以看到时间不为空)

          !times.isEmpty

          无论如何,在涉及对已检查对象的方法调用的其他检查之前,您应该检查 null。

          【讨论】:

            【解决方案7】:

            变量 not 为空。她的初始化不是空值而是空值。

            可以在 TestActivity 类的第 426 行找到 NullPointerException。我认为那一行是:

            if (scores.length != 0 && scores != null && scores.length >= 4) {
            

            因此,如果您仔细查看您的代码,scores 变量为 null,因为上述条件是错误的。

            为此更改您的代码:

            if (!times.isEmpty()) 
            {
                 scores = times.split(",");
                 Arrays.sort(scores);
            }
            if (scores != null && scores.length > 0) {
            
               ...
            
            }
            else {
            
               System.err.println("There isn't score");
            }
            

            【讨论】:

              猜你喜欢
              • 2016-07-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-15
              • 1970-01-01
              • 2016-03-20
              相关资源
              最近更新 更多