1

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

}
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
WestJackson
  • 1,067
  • 3
  • 11
  • 14

5 Answers5

5
for (int i = 0; i < animal_list.size(); i++){
    s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";
}

Because you aren't using your List, you're trying to reference the animal field in your Zoo (which you don't actually use anywhere). You have to get your Animals from your animal_list

for (int i = 0; i < animal_list.size(); i++){
    s += animal_list.get(i).getName() + " who weighs " + 
         animal_list.get(i).getWeight() + " kg.";
}

Note also that you really should be using a StringBuilder here rather than creating new String objects with the += and + operators:

public String toString() {
    StringBuilder s = new StringBuilder("In zoo "); 
    s.append(zoo_name).append(" there are the following animals:\n");
    for (int i = 0; i < animal_list.size(); i++){
        s.append(animal_list.get(i).getName());
        s.append(" who weighs ");
        s.append(animal_list.get(i).getWeight());
        s.append(" kg.\n");
    }
    return s.toString();
}

Strings are immutable in Java; you can't alter them. When you concatenate them using += or + you're actually creating new String objects and discarding the old ones. The compiler will actually optimize this away where it can to a StringBuilder, but good practice is not to leave it to the compiler.

Edit to add: In case you're not familiar, the above is an example of method chaining

When you say something like:

String name = animal_list.get(i).getName();

It's the equivalent of:

Animal a = animal_list.get(i);
String name = a.getName();
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
1

In the toString() method for the Zoo class you are calling getName() and getWeight() on an animal object that you never create.

Inside your for loop you need something like this:

 public String toString(){
    String s = "In zoo " + zoo_name + " there are the following animals:\n";
    for (int i = 0; i < animal_list.size(); i++)
    {
        animal = animal_list.get(i); //changes: 
                                     //stores the animal object at index i 
                                     //in the animal variable
        s += animal.getName() + " who weighs " + animal.getWeight() 
          + " kg.";
    }
    return s;

}

Hope this helps.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
1

Your loop here:

for (int i = 0; i < animal_list.size(); i++){
    s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";
}

accesses the member variable animal, but you've never assigned anything to that member. You probably mean to do:

for (int i = 0; i < animal_list.size(); i++){
    s += animal_list.get(i).getname() + " who weighs " + animal_list.get(i).getWeight() + " kg.";
}

This can be written more idiomaticly using the for each construct as

 for (Animal a : animal_list){
    s += a.getName() + " who weighs " + a.getWeight() + " kg.";
}
Community
  • 1
  • 1
nos
  • 223,662
  • 58
  • 417
  • 506
1

The problem is that you are not accessing the animals in your list. You are trying to print the details of the animal that you declared in the zoo class but have not instantiated. There you have your NullPointerException.

public String toString(){
  String s = "In zoo " + zoo_name + " there are the following animals:\n";
  for (Animal animal:animal_list){
    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;
 }
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
-1

why is animal a class level attribute? change the loop to for(Animal animal: animal_list) and I guess that should fix it

aishwarya
  • 1,970
  • 1
  • 14
  • 22
  • He specifically says that he doesn't want to do that. Why I can't tell you, and he left it up to reading his comment in the code to find this and the problem, but it is there. – Brian Roach Nov 25 '11 at 22:04
  • @brian, thanks for pointing it out, but i guess you should challenge his reasoning instead of down voting what would fix his problem! sorry but i would insist that he removes the class level attribute and change the loop to a for each construct. am happy to hear why that should not be done. – aishwarya Nov 25 '11 at 22:27
  • Then asking him *why* he has that requirement in a comment would be the appropriate course of action rather than making assumptions and offering an answer that was explicitly stated by the OP as not being what he wanted. Or, offering the solution he asked for, then *also* stating why using a for-each construct does the same thing. If I had to *guess* it's because this is homework, but I'd have to ask the OP. – Brian Roach Nov 25 '11 at 22:36