空指针错误有帮助吗?

时间:2011-11-25 21:44:00

标签: java list class methods nullpointerexception

嗨,我得到一个空指针,在过程中标记。测试者类位于底部。当试图打印动物园时会发生这种情况。 似乎没有为“动物动物”设置名称,但我创造了动物。

它可能正在访问我想要的错误动物,但是如何访问我已经制作的列表中的动物(请不要使用for参数中的“:”)! 我知道这是错的,但像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);

}

5 个答案:

答案 0 :(得分:5)

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

由于您没有使用List,因此您尝试引用animal中的Zoo字段(您实际上并未在任何地方使用该字段)。您必须从Animal

获取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.";
}

另请注意,您确实应该使用StringBuilder,而不是使用String+=运算符创建新的+对象:

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

String在Java中是不可变的;你无法改变它们。当您使用+=+连接它们时,您实际上正在创建新的String对象并丢弃旧对象。编译器实际上会将它优化到StringBuilder,但是好的做法是不要将它留给编译器。

编辑以添加: 如果您不熟悉,以上是method chaining

的示例

当你说出类似的话时:

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

相当于:

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

答案 1 :(得分:1)

在Zoo类的toString()方法中,您在永远不会创建的动物对象上调用getName()getWeight()

在for循环中你需要这样的东西:

 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;

}

希望这有帮助。

答案 2 :(得分:1)

你的循环在这里:

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

访问成员变量animal,但您从未向该成员分配任何内容。 你可能意思是这样做:

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

这可以使用for each构造作为

更加惯用地编写
 for (Animal a : animal_list){
    s += a.getName() + " who weighs " + a.getWeight() + " kg.";
}

答案 3 :(得分:1)

问题是您没有访问列表中的动物。您正在尝试打印在动物园类中声明但尚未实例化的动物的详细信息。你有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;
 }

答案 4 :(得分:-1)

为什么动物是一个班级属性?将循环更改为for(Animal animal: animal_list),我想应该修复它