这是我的代码。我一直在我的sort和print方法中获得nullpointerexception,它从带有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
答案 0 :(得分:2)
您有两个名为allAnimal
的变量:
static Animal[] allAnimal;
Animal[] allAnimal = new Animal[length];
您初始化一个,然后使用另一个(仍为null
)。
在getData()
中,更改
Animal[] allAnimal = new Animal[length];
到
allAnimal = new Animal[length];
可能还有其他问题,我看得太近了。
答案 1 :(得分: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
}
答案 2 :(得分:0)
您只在类级别声明了AllAnimal:
static Animal[] allAnimal;
现在,在你的getData()方法中,你声明并初始化了一个局部变量allAnimal
。请注意,它与类级别的无关。
Animal[] allAnimal = new Animal[length];
在其他方法中,您尝试使用类级别allAnimal
,它会抛出一个空指针,而不是它是初始化的。
for(i = 0; i < allAnimal.length; i++){