繁体   English   中英

如何避免空指针错误

[英]How to avoid null pointer error

我试图找到2个arrayLists的元素是否匹配。 但是此代码给我Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException错误Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException因为某些元素为空。

我该如何解决这个问题?

String level []={"High","High","High","High","High","High"};
ArrayList<Object> n = new ArrayList<Object>(Arrays.asList(level));

String choice []={null,"High","Low","High",null,"Medium"}; 
ArrayList<Object> m = new ArrayList<Object>(Arrays.asList(choice));

//Check if the two arrayList are identical
for(int i=0; i<m.size(); i++){
   if(!(m.get(i).equals(n.get(i)))){   
 result= true;
 break;
   } 
} 
    return  result;
}

只需使用Arrays.equals,如下所示:

    String level []={"High","High","High","High","High","High"};

    String choice []={null,"High","Low","High",null,"Medium"}; 

    return Arrays.equals(level, choice); 

问题是您要在不首先检查null情况下在某些元素上调用equals方法。

改成:

for(int i=0; i<m.size(); i++){
   if(m.get(i) != null && !(m.get(i).equals(n.get(i)))){   
     result = true;
     break;
   } 
} 

或者,如果要允许两个null值比较相等:

for(int i=0; i<m.size(); i++){
   if (m.get(i) == null) {
     if (n.get(i) != null) {
       result = true;
     }
   } else if(!(m.get(i).equals(n.get(i)))){   
     result = true;
   } 
   if (result) {
     break;
   }
} 

我不知道的一件事-为什么发现不匹配时将结果设置为true? 如果两个列表都匹配,是否要返回true,否则返回false?

此问题的根源可能是您使用null作为实际值。

仅查看代码即可使用enum ,而使用EMPTY值代替null。 然后,您实际上可以在没有nullpointerexceptions的列表中进行比较。

检查一下: http : //java.sun.com/docs/books/tutorial/java/javaOO/enum.html

也请尝试避免使用数组。 仅使用列表,但使用正确的类型。 不要使用几乎永远无效的List<Object>

null应该表示错误或仅测试。 永远不要在有效代码中使用它,因为您将在运行时创建空指针异常错误。

如果您知道第一个列表从不包含空值,则切换呼叫

if(!(n.get(i).equals(m.get(i)))){ 

还指定ArrayList<Object>是不好的做法,如果实际上是String对象,请使用List<String>

首先检查对象是否为同一对象(或均为null)。 在执行equals()测试之前,请检查是否为null。

    boolean result = true;
    String level[] = { "High", "High", "High", "High", "High", "High" };
    ArrayList<String> n = new ArrayList<String>(Arrays.asList(level));

    String choice[] = { null, "High", "Low", "High", null, "Medium" };
    ArrayList<String> m = new ArrayList<String>(Arrays.asList(choice));

    // Check if the two arrayList are identical
    for (int i = 0; i < m.size(); i++) {
      String mElement = m.get(i);
      String nElement = n.get(i);

      if (mElement == nElement) {
        result = true;
      } else if ((mElement == null) || (nElement == null)) {
        result = false;
        break;
      } else if (!(m.get(i).equals(n.get(i)))) {
        result = false;
        break;
      }
    }

    return result;
  }

像这样重写您的if ,以便检查双无效和单无效:

if((m.get(i) == null && n.get(i) == null) || (m.get(i) != null && !(m.get(i).equals(n.get(i)))))

与其解决这个特定问题,不如给自己一个可以反复使用的工具,例如:

public static final boolean areEqual(Object o1, Object o2) {
    return o1 == null ? o2 == null : o1.equals(o2);
}

...在一些方便的实用程序类中,然后在循环中使用它。

但是,当然,对于此特定要求, 派生具有正确的答案 (使用java.util.Arrays.equals(Object[],Object[]) )。

删除NULL

您可以在处理之前从List对象中删除NULL值。

myList.removeAll( Collections.singleton( null ) );

Collections类是一堆方便的实用程序方法。 不要与Collection (单数)混淆, Collection是父级List的接口,并由ArrayList实现。

有关更多讨论,请参见此帖子, 从Java中的列表中删除所有空值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM