2016-11-13 70 views
-2

背景:我正在爲我的一個大學課程及其相當大的項目製作模擬器。沒有看到來自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++; 
} 
+0

在world.turn()中會發生什麼?似乎是一個罪魁禍首。 –

+0

@melgart我想知道這一點,(我們必須採取一個代碼庫並修改它)是否有關係,世界被聲明爲靜態? – Clement

+0

我問,因爲它似乎像initWorld()和world.turn()可能會拋出該異常(或訪問數組),但我們看不到它。我會先探索這個代碼。有時在此過程中添加打印語句是跟蹤程序流以查看崩潰位置的最簡單方法。 –

回答

-1

嘗試使用嘗試捕捉

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()) { 
try { 
    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(); 
    } 
} 
catch(Exception e) { 
    e.printStackTrace(); // or do print full stack trace on log file 
} 
} 
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."); 

}

-1
  1. 請張貼整個工作的例子。此示例缺少複製問題所需的一半代碼。

  2. 一般來說,你應該使用Java的內置迭代器只要有可能,即

不要做:

for(int i = 0; i < board.size(); i++) { 
    List<Cell> row = board.get(i); 
... 
} 

相反,這樣做:

for(List<Cell> row : board){ 
... 
} 
  • 正如其中一條評論所述,您應該使用Eclipse調試器針對這種情況。

  • 我不確定您的canRun()方法中有什麼,但您應該在讀取它之前獲得checking whether your scanner has a next line