-1

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;
                }

            }
        }
    }
}
Grant Lee
  • 77
  • 5

3 Answers3

1

You have to check if the array element is null before comparing names with String equals method so instead of :

if(cats[index].getName() != catName) { /* logic */ }

the right condition is:

Cat cat = cats[index];
if(cat != null && !cat.getsName().equals(catName)) { /* logic */ }
dariosicily
  • 4,239
  • 2
  • 11
  • 17
1

Check if the value is equal to null. If it is not equal to null then do your condition. I made an example in the code below

        String arr[] = new String[]{"tom","blake",null,"jerry"};
        String fider = "jerry";
        for (int i = 0; i < arr.length; i++)
        {
            if (arr[i] != null)
                if (arr[i].equals(fider))
                    System.out.println(i); // Prints 3
        }
Madhu Sharma
  • 562
  • 4
  • 9
0

Use simple logic. First check the array element if null, then if not null compare the cat name. Here is an example on how you should do a null check and compare strings.

public class Cat {
    public static void main(String[] args) {

        String catName = "Cat3";
        String[] cats = new String[] {"Cat1","Cat2","Cat3", "", "Cat4"};
        searchCatByName(catName, cats);
        
    }
    
    public static void searchCatByName(String catName, String[]cats) {
    
        int catCounter = 0;
        for(int i=0;i<cats.length;i++){
            if (cats[i] != null){
                if(catName.equals(cats[i])){
                    System.out.println(catName + " is in cage no " + (i+1));
                    catCounter++;
                }
            }
        }
        if(catCounter == 0){
            System.out.println(catName + " was not found in the cattery.");
        }

    }
}
Som
  • 1,522
  • 1
  • 15
  • 48
  • 1
    I understand now. I was trying to access a null object and return the name. To avoid the NPE, I need to first check if the object is null, before I check the name. Thanks +1 – Grant Lee Jun 26 '20 at 16:29
  • @GrantLee Thanks.. added the code for Cat not found also. – Som Jun 26 '20 at 16:31