背景:我正在为我的一个大学课程及其相当大的项目制作模拟器。没有看到来自Java的完整堆栈跟踪错误
问题:但是,每当I型“I”或“c”(在代码示出的细节)我从一个的java.lang ArrayIndexOutOfBoundsException异常异常(未定制的处理程序)没有任何堆栈跟踪,并因此我找不到问题。
照片:
代码:
/**
* Creates world and starts loop of commands
*/
public void run() {
if(configFile == null) {System.exit(0);}
initWorld();
if(world == null) {
System.out.println("Unable to read config file.");
return;
}
world.print();
Scanner reader = new Scanner(System.in);
String line = reader.nextLine();
while(canRun()) {
if(line.equals("p")) {
world.print();
} else if(line.equals("c")) {
world.turn();
Species.printInfo();
steps++;
if(steps % 50 == 0) {
Species.printSummary(50);
}
} else if(line.equals("r")) {
Species.printStatus();
}
if(line.equals("i")) {
world.turn();
steps++;
if(steps % 50 == 0) {
Species.printSummary(50);
}
} else {
line = reader.nextLine();
}
}
world.print();
Species.printSummary(-1);
if(this.steps >= this.maxSteps)
System.out.println("Simulation ended because the turn limit was reached.");
else if(getPopulationOfWorld() == 0)
System.out.println("Simulation ended because all of the species died.");
else if(getChangesInPastSteps() == 0)
System.out.println("Simulation ended because there were no changes in the last 50 turns.");
}
public void turn() {
int curSteps = this.getSteps();
System.out.println("Current Steps: " + curSteps);
if(Species.getDeaths().size() == curSteps) {
Species.getDeaths().add(new ArrayList<Integer>());
for(int i = 0; i < Species.getSpecies().size(); i++) {
Species.getDeaths().get(curSteps).add(0);
}
}
if(Species.getBirths().size() == curSteps) {
Species.getBirths().add(new ArrayList<Integer>());
for(int i = 0; i < Species.getSpecies().size(); i++) {
Species.getBirths().get(curSteps).add(0);
}
}
System.out.println("Begin Turning!");
for(int i = 0; i < board.size(); i++) {
List<Cell> row = board.get(i);
for(int j = 0; j < row.size(); j++) {
Cell cell = row.get(j);
if(cell.getAnimal() != null) {
cell.getAnimal().activity();
}
if(cell.getPlant() != null) {
cell.getPlant().activity();
}
}
}
steps++;
}
在world.turn()中会发生什么?似乎是一个罪魁祸首。 –
@melgart我想知道这一点,(我们必须采取一个代码库并修改它)是否有关系,世界被声明为静态? – Clement
我问,因为它似乎像initWorld()和world.turn()可能会抛出该异常(或访问数组),但我们看不到它。我会先探索这个代码。有时在此过程中添加打印语句是跟踪程序流以查看崩溃位置的最简单方法。 –