java - 切换到不同方法时如何修复数组的 NullPointerException
问题描述
我正在用 Java 创建一个国际象棋游戏,它需要创建一个二维对象数组来制作棋盘。该数组在创建时显示为其中包含所有对象,但是当我从不同的对象调用不同的方法时,该数组突然充满了空值。
这是在 Java 中,包括在 Eclipse 中运行的一些 JavaFX(我认为 JavaFX 不会影响任何东西,但它被标记以防万一)。我试过在多个位置打印数组,它只在 createBoard() 中有效,在 setBoard() (或 GUI 的 startGame() 方法中无效。
public class ChessGame {
public static void main(String[] args) {
GUI gui = new GUI();
Application.launch(GUI.class, args);
Player player1 = new Player();
Player player2 = new Player();
gui.startGame(player1, player2);
}
}
public class GUI extends Application {
Board chessBoard = new Board();
@Override
public void start(Stage primaryStage) {
GridPane chessBoardPane = chessBoard.createBoard();
primaryStage.setScene(new Scene(chessBoardPane, 400, 400));
primaryStage.show();
}
public void startGame(Player player1, Player player2) {
//printing the array here still produces nulls.
chessBoard.setBoard(player1, player2);
}
}
public class Board {
private BoardSquare[][] boardArray;
public static int boardSize = 8;
//private GridPane boardGrid = null;
public Board() {
boardArray = new BoardSquare[boardSize][boardSize];
}
public GridPane createBoard() {
GridPane chessBoard = new GridPane();
for (int x = 0; x < boardSize; x++) {
for (int y = 0; y < boardSize; y++) {
StackPane square = new StackPane();
String color;
if ((x + y) % 2 == 0) {
color = "white";
} else {
color = "black";
}
square.setStyle("-fx-background-color: " + color + ";");
chessBoard.add(square, y, x);
boardArray[x][y] = new BoardSquare(x, y, color, square, null);
}
}
for (int i = 0; i < boardSize; i++) {
chessBoard.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
chessBoard.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
}
//at this point, printing the x and y values of the array is possible.
return chessBoard;
}
public void setBoard(Player player1, Player player2) {
Player currentPlayer = player1;
for (int x = 0; x < boardSize; x++) {
for (int y = 0; y < boardSize; y++) {
//if (boardArray[x][y] != null) { <-- error occurs if removed
if (y == 0) {
if (x == 0 || x == 7) {
Rook rook = new Rook(currentPlayer);
boardArray[x][y].setPiece(rook); //<-- error
}
//etc.
在 createBoard 中打印 boardArray 的 x 和 y 值会打印出预期的 8x8 网格的坐标。移至 setBoard() 时,boardArray 仍应充满 BoardSquare 值,但现在突然间所有值都为空,并且尝试打印数组的 x 和 y 值无济于事。我不知道为什么数组突然空了。
解决方案
你的方法有两个问题main
:
- 您创建自己的类实例,该实例与调用
GUI
启动的实例不同。永远不会为您创建的实例调用.Application.launch
start
GUI
main
Application.launch
阻塞直到应用程序退出。到那时,传递任何要在 gui 中使用的信息都为时已晚。
由创建的Application
实例Application.launch
用作应用程序的入口点。出于这个原因,您应该将这种初始化移动到start
方法中:
@Override
public void start(Stage primaryStage) {
GridPane chessBoardPane = chessBoard.createBoard();
Player player1 = new Player();
Player player2 = new Player();
this.startGame(player1, player2);
primaryStage.setScene(new Scene(chessBoardPane, 400, 400));
primaryStage.show();
}
推荐阅读
- javascript - How to add to each object in an array a key-pair value based on each objects id?
- reactjs - change "css" (in one component) based on some condition in other component? (REACT)
- azure - TableEntity 仅部分保存在 auzre 存储表中
- python - 多线程在停止后继续工作(Python
- r - 将复杂的 json 转换为数据集
- c# - 如何修复错误“设计者必须创建一个类型为“”的实例,但不能因为它标记为抽象”
- php - 像我们在使用 where in 子句的 sql 查询中所做的那样,在 laravel 中的 where 子句中使用一个查询结果的方法是什么?
- leaflet - 如何创建具有单独点击功能的多条折线?
- python - 上下文处理器无法传递数据
- java - 检查是否安装了 Android 应用程序在第二次检查时返回错误