2

I have some problem with my code.

First of all, here are my codes.

public class Zoo {
public int j=0;
public Animal[] park;

// Exercise 9
public Zoo() {
    Animal[] park = new Animal[10];
}

// Exercise 10
public void addAnimal(Animal first) {
    for (int i = 0; i < 10; i++) {
        if (park[i] != null) {
            park[i] = first;
            i=j;
            i = 10;
        } else if (i == 9) {
            System.out.println("The zoo is full!");
        }

    }
}

// Exercise 11
public void feed() {
    for (int i = 0; i < 10; i++) {
        park[i].mass *= 1.1;
    }
}

public String toString() {
    String result = "The list:\n";
    for (int i = 0; i< 10; i++) {
        result = result + "cage " + i + " status:" + park[i] + "\n"; 
    }
    return result;
}

public void print() {
    System.out.println(park.toString());
}

public int totalLegs() {
    int totalLeg = 0;
    for (int i = 0; i < 10; i++) {
        totalLeg += park[i].legs;
    }
    return totalLeg;
}
}

ALSO

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
public String toString(){
    return "name =" + name + "legs=" + legs + "mass=" + mass;
}

public void massSetter() {

}

public String getName() {
    return name;
}

public int getLegs() {
    return legs;
}
}

AND

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

    zoo.addAnimal(elephant);
    zoo.addAnimal(spider);
    zoo.addAnimal(snake);

    zoo.print();

    System.out.println("Average number of legs is");


}
}

As you probably can tell from the code, I am very new to programming and when I run the last class (TestZoo.java), it gives me the following error.

 Exception in thread "main" java.lang.NullPointerException
        at Zoo.addAnimal(Zoo.java:13)
        at TestZoo.main(TestZoo.java:9)

I did some searching and apparently I get this error when I try to pass a null as if it has something.

I looked at line 13 of the Zoo class and I honesty have no idea how to fix this.

Any help would be greatly appreciated.

Thanks in advance

1
  • Do not use i = 10 to exit the for. Use a break or return statement. Commented Nov 7, 2013 at 11:59

2 Answers 2

6

This is the problem:

public Animal[] park;

public Zoo() {
    Animal[] park = new Animal[10];
}

You're declaring an instance variable called park - but then in the constructor, instead of assigning a value to that instance variable, you're creating a local variable called park and assigning a value to that. Change your constructor to:

public Zoo() {
    park = new Animal[10];
}

Or you could change the declaration to:

public Animal[] park = new Animal[10];

and remove the explicit constructor declaration entirely.

Either way, the instance variable park will be non-null when you call addAnimal.

Additionally, within addAnimal, there are various issues. For a start, you're continuing to look through park until you find a non-null entry, rather than until you find a null entry... which is the wrong way round.

There are various other things which I'd change about the code (e.g. keep fields private), but hopefully this is enough to get you going.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you :) The code somewhat worked but now I am faced with another problem... When I run the code, it gives me the following The zoo is full! The zoo is full! The zoo is full! [LAnimal;@3a67ad79 Average number of legs is I don't know how the second last line comes out like that...
@ymc331 If you now have a new question then I would ask that seperately, but have a go at solving yourself first
The j variable was used to count the number of animals that are actually in there.
However it is caused by you calling park.toString(); unless you override the toString() method it just prints some default information about it, including (something like) a memory address). You may be attempting to print the individual animals rather than the array itself
"There are various other things which I'd change about the code (e.g. keep fields private), but hopefully this is enough to get you going." - If such suggestions interest you, post your code to codereview.SE after you get it working.
|
1

Within your constructor

public Zoo() {
    Animal[] park = new Animal[10];
}

you have shadowed park, as such the instance variable park is never initialised

This means you have created annother variable park that happens to have the same name as the class variable

public class Zoo {
public int j=0;
public Animal[] park; <--This park

    public Zoo() {
        Animal[] park = new Animal[10]; <--In not the same as this park
    }

To correct simply stop creating a new park within your constructor, so

public class Zoo {
public int j=0;
public Animal[] park; <--This park

    public Zoo() {
        park = new Animal[10]; <--Now is the same as the other park
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.