Question: I need help cleaning up my code. When I submitted my assignment I got the following results: java.lang.NullPointerException What should happen getFirstAnimal public Animal getFirstAnimal()

I need help cleaning up my code. When I submitted my assignment I got the following results:

java.lang.NullPointerException

What should happen"

  • getFirstAnimal

    public Animal getFirstAnimal()

    Returns:

    The first Animal in the array, or null if the array is empty

  • getLastAnimal

    public Animal getLastAnimal()

    This method will get the last Animal in the array

    Returns:

    Animal located at index numAnimals-1

My Code:

public Animal getFirstAnimal() { if(animals[0] == null) { return null; } else { return animals[0]; }

}

public Animal getLastAnimal() { return animals[getNumAnimals()-1]; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!