切换到其他方法时,如何为数组修复NullPointerException

时间:2019-02-10 09:36:24

标签: java arrays javafx nullpointerexception

我正在用Java创建国际象棋游戏,需要创建2D对象数组来制作国际象棋棋盘。数组在创建时显示为其中包含所有对象,但是当我从其他对象调用不同的方法时,数组突然充满了空值。

这是在Java中运行的,其中包括一些JavaFX(我不认为任何受JavaFX影响的东西,但都以防万一标记)已在Eclipse中运行。我曾尝试在多个位置打印数组,但它只能在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值无济于事。我不知道为什么数组突然变空。

1 个答案:

答案 0 :(得分:0)

您的main方法存在两个问题:

  1. 您将创建自己的GUI类实例,该实例与Application.launch调用所启动的实例不同。您从未在start中创建的GUI实例调用main
  2. 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();
}