This for loop is supposed to go through a String array and make the value hasEmpty true if the array contains either a null value or empty String. The following code gives me a NullPointerException.
String[] names = {null, "B", "C"};
boolean hasEmpty = false;
for(int i = 0; i<names.length; i++){
if ((names[i].equals("")) || (names[i] == null))
hasEmpty = true;
}
But if I change the order of the OR statement in the if clause, the following code seems to work. Can someone tell me why this is so?
String[] names = {null, "B", "C"};
boolean hasEmpty = false;
for(int i = 0; i<names.length; i++){
if ((names[i] == null) || (names[i].equals("")))
hasEmpty = true;
}