Hi i get a null pointer where marked in the process. The tester class is at the bottom. It happens when trying to printout the zoo. It seems to be the no name is set for "Animal animal" but I have created animals.
It could be accessing the wrong animal i want, but then how do I access the animals in the list I've made (please no using the ":" in the for parameter)! i know this is wrong but something like animal_list.printdetails?
public class Zoo {
private Animal animal;
public int number_animals;//# animals allowed in zoo
private List<Animal> animal_list;
private String zoo_name;
public Zoo(String name){
this.zoo_name = name;
animal_list = new ArrayList<Animal>();
}
public void addAnimal(Animal obj) {
animal_list.add(obj);
}
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (int i = 0; i < animal_list.size(); i++){
s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";//null pointer on this line why??? i have made an animal. How do I access this animals in the list (please no using the ":" in the for parameter)!
}
return s;
}
public class Animal {
public String name;
public int weight;
private String food_type;
private int space_requirement;
private Zoo zoo;
private Animal animal;
public Animal(String name, int weight){
this.name = name;
this.weight = weight;
}
public String getName(){
return this.name;
}
public int getWeight(){
return this.weight;
}
public class Test {
public static void main(String[] args) {
Zoo diego = new Zoo("San Diego");
Animal giffy = new Animal("Giffy", 950);
Animal gunther = new Animal("Gunther", 950);
diego.addAnimal(giffy);
diego.addAnimal(gunther);
System.out.println(diego);
}