2016-11-13 72 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