背景:我正在爲我的一個大學課程及其相當大的項目製作模擬器。沒有看到來自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()可能會拋出該異常(或訪問數組),但我們看不到它。我會先探索這個代碼。有時在此過程中添加打印語句是跟蹤程序流以查看崩潰位置的最簡單方法。 –