gpt4 book ai didi

Java 数组 (java.lang.NullPointerException)

转载 作者:行者123 更新时间:2023-11-29 05:37:01 24 4
gpt4 key购买 nike

我在我的 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;
}

最佳答案

这是你的构造函数中的问题:

Competitor[] competitors = new Competitor[MAX_COMPETITORS];

您不是在初始化成员变量 competitors,而是在您的构造函数中创建一个本地 competitors 变量。所以你的实例数组变量没有初始化,将为空。由于您的数组将保持未初始化状态,因此请按照此处所述更改构造函数:

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 在他的评论中指出的那样,您的代码中还有另一个问题。您没有初始化竞争对手的数组元素。如果您正在创建新元素,那么您需要为每个元素执行新操作,否则您的数组将不为空,但每个竞争对手元素实际上为空。对此类元素调用任何方法都将导致 NullPointerException。

关于Java 数组 (java.lang.NullPointerException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19131757/

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