my method is supposed to compare a cat's name from the entire cat array.
This is because my array will have nulls between created objects.
for example [0(cat 0), 1(cat 1), 2(cat 2), 3( empty null), 4(cat 4), 5(cat 5)]
I think the reason I am getting a null pointer exception is because I try to return a null and compare it to the param (String catName).
How could I avoid this while still searching the entire array?
/**
* @param catName to search for cat name
*/
public void searchCatByName(String catName) {
if (cats[0] != null) {
if(catName != null) {
int index = 0;
while (index < cats.length) {
if(cats[index].getName() != catName) {
if(index == cats.length - 1) {
System.out.println(catName + " was not found in the cattery.");
break;
}
index++;
}
else {
System.out.println(cats[index].getName() + " is in cage " + index);
break;
}
}
}
}
}