gpt4 book ai didi

java - 无法处理二维对象数组

转载 作者:行者123 更新时间:2023-11-29 04:39:32 25 4
gpt4 key购买 nike

<分区>

我在寻址对象的二维数组时遇到问题...

我有一个 GameEngine 类,我在其中声明:

Tile[][] theBoard;

在类的后面,我设置了白板:

theBoard = new Tile[8][8];
prepareTheBoard();

prepareTheBoard 方法:(也在同一个 calss - GameEngine 中声明)

public void prepareTheBoard(){

int n = 0;

for(n = 0; n < 8; n++){
System.out.println("n: " + n + " length: " + theBoard[1].length);
System.out.println("theBoard : " + theBoard[1][1].isEmpty());
theBoard[1][n].setPiece(new Piece(PieceColor.WHITE, Pieces.PAWN, theBoard[1][n]));
theBoard[6][n].setPiece(new Piece(PieceColor.BLACK, Pieces.PAWN, theBoard[6][n]));
}
...
}

第一张打印品给我(如预期):

n: 0 length: 8

但是第二次打印报错:

Exception in thread "main" java.lang.NullPointerException

我做错了什么?为什么它看到数组的长度,但我无法解决它?

提前致谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com