Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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..

share|improve this question
3  
Which is line 16? Please learn to use a debugger - this will save you hours of your life. This forum isn't a debugging service, when you encounter a concrete issue with an SSCCE we will be happy to help. Until then... –  Boris the Spider Nov 10 at 12:12
 
You forgot to ask a question… –  anotherdave Nov 10 at 12:14
1  
To become a successful developer, you need to badly learn how to use IDE's debugger. –  Pradeep Simha Nov 10 at 12:14
1  
Put some more effort into describing your problem, before you expect others to put effort in it. Just posting a whole bunch of code is not the way to go. –  popovitsj Nov 10 at 12:15

closed as off-topic by Boris the Spider, Pradeep Simha, PearsonArtPhoto, CoverosGene, Aniket Thakur Nov 10 at 19:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – Boris the Spider, Pradeep Simha, PearsonArtPhoto, Aniket Thakur
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

This time the problem is in the extArray method:

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

It's fundamentally the same problem as last time though. You set your instance variable to null - and then introduce a new local variable called park.

So when your method returns, the park instance variable is null.

Even aside from that though, it's not at all clear what your method is trying to do. Why are you creating two extra arrays? And why are you copying null references in each loop? The latter seems to be confusion about what the assignment operator does again...

Given that you don't actually use the return value of the method, you might as well make it a void method - but you do need to fix the method itself. I'd probably make it something like:

private void extendArray() {
    Animal[] newArray = new Animal[getExtendedLength()];
    for (int i = 0; i < park.length; i++) {
        newArray[i] = park[i]; // Notice which side is which here!
    }
    park = newArray; // Not declaring a new local variable
}

Or even more simply:

private void extendArray() {
    Animal[] newArray = new Animal[getExtendedLength()];
    System.arraycopy(park, 0, newArray, 0, park.length);
    park = newArray; // Not declaring a new local variable
}

(Note that I've changed the names as well, to make them easier to read, and made the method private.)

Additionally, you're currently using recursion in your add method, if you need to extend the array. While that can work, I personally wouldn't do it - it makes it more complicated than it needs to be.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.