异常错误扫雷游戏

时间:2012-02-11 03:04:27

标签: java awt multidimensional-array minesweeper

您好我正在尝试创建一个扫雷游戏,我有我的gui并使用二维数组来存储一个位置是否有一个地雷,但是当我试图让游戏结束点击一个我使用这段代码:

if (board[row][col] == 1) {
    return GameStatus.Lost; }  
else {
    return GameStatus.Continue;
 }

我收到错误

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at Game.getGameStatus(Game.java:55)
at MineSweeperPanel$ButtonListener.actionPerformed(MineSweeperPanel.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

3 个答案:

答案 0 :(得分:0)

检查您在rowcol中调用的界限。例如,如果您有25行和列,并且您指的是board[25][25],那么这超出了数组的范围。虽然行数的总大小为25,但在数组中,索引将从0变为25-1

答案 1 :(得分:0)

数组索引超出界限意味着你的数组中有(比方说)10个元素,但你试图访问(比如说)第11个元素 - 它只是不存在。

完整性检查 - 数组从0开始编制索引,rowcol中的值是否从1索引?

答案 2 :(得分:0)

何时发生?

当您尝试访问索引超过其长度的数组时,会发生超出范围的异常。 java数组的最大索引是(长度-1) 例如:

String [] stringArray = new String[10];
stringArray[10]
// the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.

如果您不知道数组的大小或长度,可以从stringArray.length了解它。

如何处理?

您应该确保您的程序不能访问索引大于length - 1的数组。 例如:

for(int i=0;i<stringArray.lenght;i++) {
    //write your code here
}

上述代码将保证stringArray永远不会超出其最大索引。

你的案例

在您的情况下,您必须已定义数组限制并尝试访问超出定义限制范围的数组数据。

另请阅读this了解更多信息......

Example of 2d array having out of bound exception