0

I'm trying to make chess game with java only using Board class as GUI, Game class to run and test game rules, abstract Pieces as a parent class for each chess piece, Player class to indicate who's turn. I populated a board with 8 x 8 = 64 buttons first. To implement each piece's valid movement, I need to get each button's address or an index. Initially I didn't use 2d array but I thought it'd be easier to put the buttons in 2d array and get their index to control the movement of the pieces. This is what I tried so far and I don't know why I'm getting a NullPointer error.

I would appriciate any help or advice.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Board extends Application {
    GridPane board = new GridPane();
    Scene s = new Scene(board);
    Button buttonArray[][];
    private void showBoard() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                buttonArray[i][j] = new Button();


                buttonArray[i][j].setMinSize(70, 70);



                if ((i + j) % 2 == 0) { 
                    buttonArray[i][j].setStyle("-fx-background-color: #0e0f0f");
                } else {
                    buttonArray[i][j].setStyle("-fx-background-color: #ededed");
                }
                buttonArray[i][j].setOnAction(this::onSelect);

                board.add(buttonArray[i][j], i, j);
            }
        }
    }


    // event listener
    private void onSelect(ActionEvent event) {
        Button b = (Button) event.getSource();
        System.out.println("get source" + b);
        b.setStyle("-fx-background-color: magenta");

    }

    @Override
    public void start(Stage stage) throws Exception {
        showBoard();
        stage.setScene(s);
        stage.show();
    }

    public static void main(String args[]) {
        launch(args);
    }

}

//Error stackTrace

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at Board.showBoard(Board.java:15)
    at Board.start(Board.java:45)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application Board
raven39
  • 135
  • 1
  • 7
  • 1
    You don't appear to initialize `buttonArray`? – Slaw Feb 05 '19 at 06:46
  • @Slaw oh my god you were right. Thank you! By the way, do you think it's enough to make a chess game out of those 4 classes? Board(GUI), Game(rules), Player, Pieces classes. – raven39 Feb 05 '19 at 07:17
  • 1
    The array needs to be initialized before you access any of it's elements: `buttonArray[][] = new Button[8][8];` – fabian Feb 05 '19 at 09:59

0 Answers0