Reputation: 532
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
Upvotes: 2
Views: 745
Reputation: 1503509
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.
Upvotes: 6
Reputation: 17236
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
}
Upvotes: 1