我正在尝试编译并运行此代码,我已经在非常类似的线程中尝试了一个解决方案,但我仍然得到错误。在另一个帖子中发布的解决方案是使用“.add(this)”。
import java.util.ArrayList;
import java.util.Arrays;
public class Chore implements Priority{
private String description;
private int priority;
private boolean completed;
private static ArrayList<Chore> chores;
public Chore(){
String choreArr[] = {"Mow the lawn", "Fold the laundry", "Make dinner", "Vacuum the house"};
int randChore = (int)(Math.random()*4);
int randPri = (int)(Math.random()*10)-1;
description = choreArr[randChore];
priority = randPri;
chores.add(this);
}
public Chore(int i, String s){
priority = i;
description = s;
chores.add(this);
}
public void setComplete(){
completed = true;
}
public void setPriority(int p){
priority = p;
}
public int getPriority(){
return priority;
}
public String toString(){
return description + ": " + completed;
}
public int compare(Chore cC){
return priority - cC.priority;
}
public static String doFirst(){
Chore[] copy = new Chore[chores.size()];
chores.toArray(copy);
Arrays.sort(copy);
return copy[copy.length -1].description;
}
}
当我尝试创建Chore对象时,我收到以下错误
Exception in thread "main" java.lang.NullPointerException
at Chore.<init>(Chore.java:16)