当没有无限递归方法时,是什么导致了这个 Stack Overflow 错误?

What's causing this Stack Overflow error when there is no infinite recursive method?

我正在 Java 中制作国际象棋程序。我有一个布尔方法,它以两个 int 的形式获取用户想要将车移动到的位置,并根据车的当前行和列确定车是否可以移动到那里,使用 for 循环。这里以其中一个循环为例。

int nc = col - 1;
    while (nc >= 0){
    moves.add(new Integer[]{row, nc});
    if (locals[row][nc] != null)
        break;
    nc--;
}

moves是我在程序前面声明的ArrayList。它存储所有有效移动的列表,这是我用来实例化它的 for 循环之一。

问题是,每次我 运行 这段代码时,包含 add 方法的行被突出显示为无限循环,并且代码不会 运行。我做错了什么?

编辑:

这是软件向我显示的确切错误消息:

此外,我将post该方法的全文。我不确定它是否与我的问题相关,但它可能会有所帮助。

public boolean isValidMove(int r, int c){
        Piece[][] locals = Chess.getBoard();
        if (r < 0 || c < 0 || r > 7 || c > 7 || (locals[r][c] != null && locals[r][c].getWhite() == isWhite))
            return false;
        ArrayList<Integer[]> moves = new ArrayList<Integer[]>();
        int nc = col - 1;
        while (nc >= 0){
            moves.add(new Integer[]{row, nc});
            if (locals[row][nc] != null)
                break;
            nc--;
        }
        nc = col + 1;
        while (nc < 8){
            moves.add(new Integer[]{row, nc});
            if (locals[row][nc] != null)
                break;
            nc++;
        }
        int nr = row - 1;
        while (nr >= 0){
            moves.add(new Integer[]{nr, col});
            if (locals[nr][col] != null)
                break;
            nr--;
        }
        nr = row + 1;
        while (nr < 8){
            moves.add(new Integer[]{nr, col});
            if (locals[nr][col] != null)
                break;
            nr++;
        }
        for (Integer[] ints : moves){
            if (ints[0] == r && ints[1] == c)
                return true;
        }
        return false;
    }

我发现程序出了什么问题并设法修复了它。有问题的循环实际上并没有永远迭代,但是这个程序中某处的一个方法调用了另一个方法,这个方法又调用了原始方法。因此,堆栈溢出错误没有任何 one 无限递归方法。