2011-11-25 121 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; 
}