根据我的编译器,我得到了一个NullPointerException
开启,chart[rowcount][columncount] = seat;
但我已经在之前的行中初始化了座位变量。我正在尝试创建一个多变量座位数组,并使用 for 循环用座位填充它。我刚刚问了一个关于这个的问题,并认为我知道如何避免这些,但我想不是。我该如何解决这个 nullPointerException?
public class SeatChart {
private Seat chart[][];
SeatChart(double input[][]) {
Seat seat;
for (int rowcount = 0; rowcount < input[0].length; rowcount++) {
for (int columncount = 0; columncount < input[1].length; columncount++) {
seat = new Seat(input[rowcount][columncount]);
chart[rowcount][columncount] = seat;
}
}
}
public String buySeat(int row, int column) {
try {
chart[row][column].markSold();
return "Seat [" + row + "]" + "[" + column + "] was purchased.";
} catch (ArrayIndexOutOfBoundsException e) {
return "The seat your tried to purchase does not exist.";
}
}
public String buySeat(double price) {
int k = 0;
for (int rowcount = 0; rowcount < chart[0].length; rowcount++) {
for (int columncount = 0; columncount < chart[1].length; columncount++) {
if (k == 0) {
if (chart[rowcount][columncount].getPrice() == price) {
chart[rowcount][columncount].markSold();
return "Seat [" + rowcount + "]" + "[" + columncount + "] was purchased.";
}
}
}
}
return "There was an error, please try again";
}
}