How to avoid null pointer error
Posted
by Jessy
on Stack Overflow
See other posts from Stack Overflow
or by Jessy
Published on 2010-04-01T16:28:14Z
Indexed on
2010/04/01
16:33 UTC
Read the original article
Hit count: 452
I trying to find whether the elements of 2 arrayLists are match or not.
But this code give me error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
since some of the elements are null.
How can I solved this problem?
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;
}
© Stack Overflow or respective owner