2011-11-25 122 views
1

嗨,我得到一个空指针在过程中标记的地方。测试仪类在底部。它试图打印出动物园时发生。 它似乎是没有名字是为“动物动物”设置,但我创造了动物。空指针错误的帮助?

它可能会访问我想要的错误动物,但是如何访问列表中的动物(请勿使用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); 

} 
+1

http://docs.oracle.com/javase/6/docs/api/java/util/List.html#get%28int%29 –

回答

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

因为你不使用你的List,你想引用你的Zooanimal场(你不实际使用的任何地方)。你必须从你的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."; 
} 

注意让你Animal S还,你真的应该在这里使用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

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

+0

他明确表示,他并不想这样做。为什么我不能告诉你,他把它留在阅读他在代码中的评论来找到这个问题,但它在那里。 –

+0

@brian,谢谢你指出,但我想你应该挑战他的推理,而不是投下来什么可以解决他的问题!对不起,但我会坚持说他删除了类级别属性并将每个构造的循环更改为a。很高兴听到为什么不应该这样做。 – aishwarya

+0

然后问他*为什么*他在评论中有这样的要求将是适当的行动方式,而不是做出假设,并提供OP所明确表达的不是他想要的答案。或者,提供他要求的解决方案,然后*还*说明为什么使用for-each构造做同样的事情。如果我必须*猜测*这是因为这是作业,但我不得不问OP。 –

1

在为你的动物对象,你从来没有创建调用getName()getWeight()动物园类toString()方法。

内,您的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; 

}

希望这有助于。

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

这可以更idiomaticly使用for each结构写成

for (Animal a : animal_list){ 
    s += a.getName() + " who weighs " + a.getWeight() + " kg."; 
} 
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; 
}