【问题标题】:Why does my string array have null values? (java) [duplicate]为什么我的字符串数组有空值? (java) [重复]
【发布时间】:2012-10-18 05:13:22
【问题描述】:

可能重复:
how to compare elements in a string array in java?

我正在尝试将数组中的单词设置为字符串,然后计算不包括重复项的单词数,但是当我尝试使用 if 语句检查重复项时,它显示“NullPointerException”意味着存在数组中为空。为什么数组有空值?

这里是设置字符串数组的代码,输入是“DO UNTO OTHERS AS YOU WOW HAVE THEM DO UNTO YOU”:

   String[] stringArray = new String[wordCount];
   while (!line.equals("DONE"))
   {
       for ( int k = 0 ; k < wordCount ; k++)
       {
           //put tokens into string array
           StringTokenizer tokens = new StringTokenizer(line);
           stringArray[k] = tokens.nextToken();
       }
   }

这里是比较代码和导致 NullPointerException 的 if 语句:

   for ( int j = 0 ; j < wordCount ; j++)
   {    
       for (int i = j+1 ; i < wordCount ; i++)
       {       
            if (stringArray[i] == null)
            {
                stringArray[i] = "null";
            } 
            else if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j) 
            {
                //duplicate
                System.out.print("\n" + stringArray[j]);
                duplicates++;
            }
       }
   }
   wordCount -= duplicates;
   System.out.print("\nNumber of words, not including duplicates: " + wordCount);

我正在尝试空检查,但结果仍然很差,因为它增加了更多重复项,因为当我将 stringArray[i] 更改为“null”时,它也会更改 stringArray[j]

请帮忙!我一直在努力解决这个问题

【问题讨论】:

  • 发布完整的堆栈跟踪会很有帮助。
  • 空指针异常发生在哪一行?
  • 你试过调试它吗?你发现了什么?
  • 您在循环的每次迭代中都创建了 StringTokenizer。

标签: java arrays string null nullpointerexception


【解决方案1】:

您应该使用equals() 而不是compareTo()compareTo() 引发 NullPointerException 如果传递 null。

Comparable的Java文档

注意 null 不是任何类的实例,即使 e.equals(null) 返回 false,e.compareTo(null) 也应该抛出 NullPointerException。

          if (stringArray[i] == null) {
                continue;
            } else if (stringArray[i].equals(stringArray[j]) && i != j) {
                // duplicate
                duplicates++;
            }

所以完整的源代码应该是这样的。 I have not changed any logic inside your for loop.

    String line = "DO UNTO OTHERS AS YOU WOULD HAVE THEM DO UNTO YOU";
    String[] stringArray = line.split("\\s");//Use split StringTokenizer use is discouraged in new code
    int duplicates = 0;
    int wordCount = stringArray.length;
    for (int j = 0; j < stringArray.length; j++) {
        for (int i = j + 1; i < stringArray.length; i++) {
            if (stringArray[i] == null) {
                stringArray[i] = "null";
            } else if (stringArray[i].equals(stringArray[j]) && i != j) {
                // duplicate
                System.out.print("\n" + stringArray[j]);
                duplicates++;
            }
        }
    }
    wordCount -= duplicates;
    System.out.print("\nNumber of words, not including duplicates: "
            + wordCount);

【讨论】:

  • 是的 - 但行为的文档参考?
  • 我试过 equals() 仍然得到 NullPointerException
  • @凯瑟琳。检查代码sn-p。
【解决方案2】:

删除您的 if (stringArray[i] == null) 空检查。这不是必需的。相反,请尝试在初始化后立即打印出数组,并查看 wordCount 是否可能太大,或者标记器可能没有破坏您认为应该的字符串。

【讨论】:

    【解决方案3】:

    @AmitD 的代码看起来很棒!!我猜你的代码唯一的问题是关于 wordcount..我认为你没有初始化可能给你异常的字符串数组,这就是我所理解的..只需用一些启动 wordcount价值..检查字数是否有所增加..可能会有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-16
      • 2021-04-11
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2019-02-27
      相关资源
      最近更新 更多