2013-10-26 30 views
1

這裏是我的代碼。在我的排序和打印方法中,我始終得到一個nullpointerexception,它開始於allAnimals.length的for循環。我認爲該類中的allAnimal數組,並沒有填充getData,因爲它應該。 getData方法中的allAnimal不被視爲類中的allAnimal。基本上所有其他方法仍然認爲allAnimal爲空。我不是java的主人,所以如果有人可以請告訴我如何解決這個錯誤,或給我提示如何避免它,我將不勝感激。在java中修復nullpointerexception錯誤

public class Animal { 
//data fields 
private String name; 
private int birthYear; 
private String species; 
private float balance; 
private String ownersName; 
static Animal[] allAnimal; 


public Animal(){ 
    //no-arg constructor 
} 

public Animal(String name, int birthYear, String species, float balance, String ownersName){ 
    // constructor builds animal template 
    this.name = name; 
    this.birthYear = birthYear; 
    this.species = species; 
    this.balance = balance; 
    this.ownersName = ownersName; 
} 


//set and get for name 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

//set and get for birth year 
public int getBirthYear() { 
    return birthYear; 
} 

public void setBirthYear(int birthYear) { 
    this.birthYear = birthYear; 
} 

//set and get for species 
public String getSpecies() { 
    return species; 
} 

public void setSpecies(String species) { 
    this.species = species; 
} 

//set and get for balance 
public float getBalance() { 
    return balance; 
} 


public void setBalance(float balance) { 
    this.balance = balance; 
} 

//set and get for owner 
public String getOwnersName() { 
    return ownersName; 
} 

public void setOwnersName(String ownersName) { 
    this.ownersName = ownersName; 
} 







public static void getData(){ 

    System.out.println("How many animals are in this report? "); 
    Scanner kb = new Scanner(System.in); 

    int length = kb.nextInt(); 
    Animal[] allAnimal = new Animal[length]; 

    System.out.println("input: animal name, birth year, species, bill balance and owner's name."); 
    //fill array of objects with data 
    int i; 
    for(i = 0; i < allAnimal.length; i++){ 

     allAnimal[i] = new Animal(kb.next(), kb.nextInt(), kb.next(), kb.nextFloat(), kb.next()); 
    } 


}//end getData 


public static void sortData(Animal[] allAnimal){ 
    Animal temp; 
    int i,j; 
    for(i = 0; i < allAnimal.length; i++){ 
     for(j = i + 1; j < allAnimal.length; j++){ 


      if(allAnimal[i].getBalance() > allAnimal[j].getBalance()){ //swap big with small 
       temp = allAnimal[j]; 
       allAnimal[j] = allAnimal[i]; 
       allAnimal[i] = temp; 
      } 
     } 
    } 
}//end sortData 


    public static void printData(Animal[] allAnimal){ 
     int i; 
     for(i = 0; i < allAnimal.length; i++){ 
     System.out.println("Pet Name: " + allAnimal[i].getName() + " Birth year: " + 
       allAnimal[i].getBirthYear() + " Species: " + allAnimal[i].getSpecies() + 
       " Balance due: " + allAnimal[i].getBalance() + " Owner: " + allAnimal[i].getOwnersName()); 
     } 
    } 


    public static void main(String[] args){ 
     getData(); 
     sortData(allAnimal); 
     printData(allAnimal); 

    }//end main 

}//end class 

回答

2

你有兩個變量命名allAnimal

static Animal[] allAnimal; 
Animal[] allAnimal = new Animal[length]; 

您初始化一個,然後用其他的(這仍然是null)。

getData(),改變

Animal[] allAnimal = new Animal[length]; 

allAnimal = new Animal[length]; 

可能有其他的問題,我也沒看太密切。

0

您正在創建局部變量Animal[] allAnimal,導致問題的方法爲getData()

問題就在這裏

public static void getData(){ 

    Animal[] allAnimal = new Animal[length]; //here is the problem 
} 

修復它像下面。

public static void getData(){ 

    allAnimal = new Animal[length]; //here is the fix 
} 
0

你只宣佈AllAnimal類級別:現在

static Animal[] allAnimal; 

,在您的getData()方法你已經聲明並初始化一個局部變量allAnimal。請注意,它與課堂級別無關。

Animal[] allAnimal = new Animal[length]; 

在其他的方法,你要使用一流水平allAnimal inless它initiatlized這將拋出一個空指針。

for(i = 0; i < allAnimal.length; i++){