I have the following codes
public class Zoo {
public Animal[] park;
int numberOfAnimals = 0;
boolean full;
// Exercise 9
public Zoo() {
park = new Animal[10];
}
// Exercise 10
public void addAnimal(Animal ani) {
for (int i = 0; i < park.length; i++) {
if (park[i] != null && i == park.length - 1) {
System.out.println("The zoo is full!");
full = true;
extArray();
addAnimal(ani);
} else if (park[i] == null) {
park[i] = ani;
numberOfAnimals++;
break;
}
}
}
// Exercise 11
public void feed() {
for (int i = 0; i < 10; i++) {
park[i].mass *= 1.1;
}
}
@Override
public String toString() {
String str = "The zoo is capable of keeping "
+ park.length
+ " animals\nThe following is the list of animals currently in the zoo.";
for (int i = 0; i < park.length; i++)
if (park[i] != null)
str += '\n' + "cage " + (i + 1) + " status: " + "Name: "
+ park[i].name + " Weight: " + park[i].mass
+ " #Legs: " + park[i].legs;
return str;
}
public void print() {
System.out.println(toString());
}
public int totalLegs() {
int totalLeg = 0;
for (int i = 0; i < 10; i++) {
if (park[i] != null)
totalLeg += park[i].legs;
}
return totalLeg;
}
public double AverageLegs() {
double avgLeg = (double) totalLegs() / numberOfAnimals;
return avgLeg;
}
public void makeBaby() {
int j;
for (int i = 0; i < numberOfAnimals; i++) {
if (i == park.length - 1 && park[i] != null) {
System.out.println("The zoo is full. No baby");
break;
} else {
// System.out.print(z);
if (i != park.length - 1) {
for (j = i + 1; j < numberOfAnimals; j++) {
if ((park[i].name).equals(park[j].name)) {
float avgMassBaby = (float) 0.1
* ((park[i].mass + park[j].mass) / 2);
Animal baby = new Animal(avgMassBaby, park[i].name,
park[i].legs);
addAnimal(baby);
break;
}
}
break;
}
}
}
}
public void removeAnimal(Animal random) {
for (int i = 0; i < park.length; i++) {
if (park[i] != null && park[i] == random) {
park[i] = null;
}
}
}
public void reorder() {
int i = 0;
while (park[i] != null) {
i++;
}
for (int j = i + 1; j < park.length; j++) {
park[i] = park[j];
i++;
if (j == park.length) {
park[j] = null;
}
}
}
public int getLengthext(){
double temp = Math.ceil(park.length * 1.5);
int extlength = (int) temp;
return extlength;
}
public Animal[] extArray() {
Animal[] parkTemp = new Animal[getLengthext()];
for(int i=0; i<park.length; i++){
park[i]=parkTemp[i];
}
park = null;
Animal[] park = new Animal[parkTemp.length];
for(int i=0; i<parkTemp.length; i++){
parkTemp[i]=park[i];
}
return park;
}
}
AND
public class Animal {
float mass;
String name;
int legs;
// Exercise 6-6
public Animal(String randomName) {
name = randomName;
legs = 0;
mass = 0;
}
// Exercise 6-7
public Animal(float one, String two, int three) {
mass = one;
name = two;
legs = three;
}
//Exercise 7
@Override
public String toString(){
return "name =" + name + "legs=" + legs + "mass=" + mass;
}
public void setMass(int mass) {
this.mass = mass;
}
public String getName() {
return name;
}
public int getLegs() {
return legs;
}
}
and finally
public class TestZoo {
public static void main(String[] args){
Zoo zoo = new Zoo();
Animal elephant = new Animal(300f,"elephant",4);
Animal spider = new Animal(0.5f,"spider",6);
Animal snake = new Animal(10f,"snake",0);
Animal panda = new Animal(50f,"panda",4);
Animal panda2 = new Animal(70f,"panda",4);
Animal panda3 = new Animal(70f,"panda2",4);
Animal panda4 = new Animal(70f,"panda2",4);
Animal panda5 = new Animal(70f,"panda3",4);
Animal panda6 = new Animal(70f,"panda3",4);
Animal panda7 = new Animal(70f,"panda3",4);
Animal panda8 = new Animal(70f,"panda3",4);
Animal panda9 = new Animal(70f,"panda3",4);
Animal panda10 = new Animal(70f,"panda3",4);
Animal panda11 = new Animal(70f,"panda3",4);
zoo.addAnimal(elephant);
zoo.addAnimal(spider);
zoo.addAnimal(snake);
zoo.addAnimal(panda);
zoo.addAnimal(panda2);
zoo.addAnimal(panda3);
zoo.addAnimal(panda4);
zoo.addAnimal(panda5);
zoo.addAnimal(panda6);
zoo.addAnimal(panda7);
zoo.addAnimal(panda8);
zoo.addAnimal(panda9);
zoo.addAnimal(panda10);
zoo.addAnimal(panda11);
zoo.makeBaby();
zoo.print();
System.out.println("Average number of legs is " + zoo.AverageLegs());
}
}
When I run the program, I get the following error
The zoo is full!
Exception in thread "main" java.lang.NullPointerException
at Zoo.addAnimal(Zoo.java:16)
at Zoo.addAnimal(Zoo.java:21)
at TestZoo.main(TestZoo.java:34)
I thought I solved this nullpointer exception problem before but I simply cannot fix this one..