gpt4 book ai didi

java - 修复java中的nullpointerException错误

转载 作者:行者123 更新时间:2023-11-30 04:10:58 25 4
gpt4 key购买 nike

这是我的代码。我在排序和打印方法中不断收到空指针异常,它从带有 allAnimals.length 的 for 循环开始。我认为类中的数组 allAnimal 没有按应有的方式填充 getData 。 getData 方法中的 allAnimal 不被视为类中的 allAnimal。基本上所有其他方法仍然认为 allAnimal 为 null。我不是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

最佳答案

您有两个名为 allAnimal 的变量:

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

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

getData()中,更改

Animal[] allAnimal = new Animal[length]; 

allAnimal = new Animal[length]; 

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

关于java - 修复java中的nullpointerException错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19603891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com