1

这是我的代码。我在我的排序和打印方法中不断收到一个空指针异常,它从带有 allAnimals.length 的 for 循环开始。我认为类中的数组 allAnimal 没有按照应有的方式填充 getData 。getData 方法中的 allAnimal 不会被视为类中的 allAnimal。基本上所有其他方法仍然认为 allAnimal 为空。我不是java大师,所以如果有人能告诉我如何解决这个错误,或者给我如何避免它的提示,我将不胜感激。

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
4

3 回答 3

2

您有两个变量名为allAnimal

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

您初始化一个,然后使用另一个(仍然是null)。

getData(), 改变

Animal[] allAnimal = new Animal[length]; 

allAnimal = new Animal[length]; 

可能还有其他问题,我没有仔细看。

于 2013-10-26T07:01:40.977 回答
0

您正在 导致问题Animal[] allAnimalgetData()方法中创建局部变量。

问题在这里

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
}
于 2013-10-26T07:04:15.390 回答
0

您只在类级别声明了 AllAnimal :

static Animal[] allAnimal;

现在,在您的方法 getData() 中,您已经声明并初始化了一个局部变量allAnimal。请注意,它与班级级别的无关。

Animal[] allAnimal = new Animal[length]; 

在其他方法中,您尝试使用allAnimal将抛出空指针的类级别,除非它被初始化。

for(i = 0; i < allAnimal.length; i++){
于 2013-10-26T07:04:49.570 回答