1

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

}

4

2 回答 2

2

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

Competitor[] competitors = new Competitor[MAX_COMPETITORS];

您不是在初始化,member variable competitors而是local competitors variable在构造函数中创建 a 。因此,您的实例数组变量未初始化,将为空。由于您的数组将保持未初始化,因此请按照此处所述更改构造函数:

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。

于 2013-10-02T07:18:28.070 回答
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;
}
于 2013-10-02T07:20:29.437 回答