我在我的java代码上收到此错误,而我似乎无法弄清楚我做错了什么..
java.lang.NullPointerException
at Race.toString(Race.java:94)
at TestA2Classes.start(TestA2Classes.java:39)
at TestA2.main(TestA2.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这是其中一个java文件的代码。问题似乎是我使用数组的部分。 我做错了什么?
public class Race {
public static final String[] RACE_DESCRIPTIONS = {"Sprint", "Distance", "Eliminator", "Keirin"};
public static final int SPRINT = 0;
public static final int DISTANCE = 1;
public static final int ELIMINATOR = 2;
public static final int KEIRIN = 3;
public static final int MAX_COMPETITORS = 8;
private int number;
private int typeIndex;
private MyDate date;
private boolean hasFinished;
private Competitor[] competitors;
private int numberOfCompetitors;
public Race(int number, int typeIndex, MyDate date) {
// TODO
this.number = number;
this.typeIndex = typeIndex;
this.date = date;
this.hasFinished = false;
this.numberOfCompetitors = 0;
Competitor[] competitors = new Competitor[MAX_COMPETITORS];
}
public int getNumber() {
// TODO
return number;
}
public boolean getHasFinished() {
// TODO
return hasFinished;
}
public int getTypeIndex() {
// TODO
return typeIndex;
}
public MyDate getDate() {
// TODO
return date;
}
public Competitor getCompetitor(int number) {
// TODO
Competitor competitor = competitors[0];
for(int i=0; i<competitors.length; i++){
if(competitors[i].getNumber() == number){
return competitor;
}
}
return null;
}
public void finishRace(int first, int second, int third) {
// TODO
this.hasFinished = true;
for(int i=0; i<competitors.length; i++){
if(competitors[i].getNumber() == first){
competitors[i].setPosition(1);
} else if(competitors[i].getNumber() == second){
competitors[i].setPosition(2);
} else if(competitors[i].getNumber() == third){
competitors[i].setPosition(3);
} else{
competitors[i].setPosition(0);
}
}
}
public boolean addCompetitor(Competitor competitor) {
// TODO
if(numberOfCompetitors < MAX_COMPETITORS){
numberOfCompetitors++;
return true;
}
return false;
}
public String toString() {
// TODO
String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + " [" + date + "]";
if(hasFinished = false){
details += ": Race not finished";
} else if(hasFinished = true){
details += "\n 1st: " + competitors[1].getName();
details += "\n 2nd: " + competitors[2].getName();
details += "\n 3rd: " + competitors[3].getName();
}
return details;
}
}
答案 0 :(得分:2)
以下是构造函数中的问题:
Competitor[] competitors = new Competitor[MAX_COMPETITORS];
您没有初始化member variable competitors
,而是在构造函数中创建local competitors variable
。因此,您的实例数组变量未初始化,并且将为null。由于您的数组将保持单元化,因此请按照此处所述更改构造函数:
public Race(int number, int typeIndex, MyDate date) {
// TODO
this.number = number;
this.typeIndex = typeIndex;
this.date = date;
this.hasFinished = false;
this.numberOfCompetitors = 0;
this.competitors = new Competitor[MAX_COMPETITORS];
}
正如Rohit Jain在评论中指出的那样,您的代码中还有另一个问题。您没有初始化竞争对手的数组元素。如果要创建新元素,则需要为每个元素执行新元素,否则您的数组将不为null,但每个竞争对手元素实际上将为null。在这些元素上调用任何方法都会导致NullPointerException。
答案 1 :(得分:1)
问题1
在构造函数中移除并将其移动到声明数组的位置
Competitor[] competitors = new Competitor[MAX_COMPETITORS];
当然,在使用它之前,你必须填充它们。
问题2
toString()
方法中的
if(hasFinished = false){
details += ": Race not finished";
} else if(hasFinished = true){
details += "\n 1st: " + competitors[1].getName();
details += "\n 2nd: " + competitors[2].getName();
details += "\n 3rd: " + competitors[3].getName();
}
您正在if
条件中初始化布尔变量。不比较。
那应该是
public String toString() {
String details = number + ", " +
RACE_DESCRIPTIONS[typeIndex] + " [" + date + "]";
if(!hasFinished){
details += ": Race not finished";
} else{
details += "\n 1st: " + competitors[1].getName();
details += "\n 2nd: " + competitors[2].getName();
details += "\n 3rd: " + competitors[3].getName();
}
return details;
}