0

These methods should ask for the size of the array (without error) then every element in the array should be false (without error) and at the end the array should be printed... but I get an exception in the line 29 (it is marked in the code)

I am initializing the array so I don't really know whats wrong with my code.

private boolean[][] board;     // true = queen, false = empty
[...]

 private void determineBoardSize(){
        write("Sprechen sie sich ab wer weiß und wer schwarz ist");
        write("weiß muss eine Zahl zwischen 5 und 8 wählen");
        nrRows = readInt("Zahl aus {5,6,7,8}");
        if(nrRows < 5 || nrRows > 8) determineBoardSize();
        write("Schwarz muss nun eine Zahl zwischen ErsteZahl -1 und ErsteZahl + 1 wählen");
        nrColumns = readInt("eine Zahl zwischen ErsteZahl -1 und ErsteZahl + 1 wählen");
        if(nrColumns < nrRows - 1 || nrColumns > nrRows + 1) determineBoardSize();

 private void initBoard(){
        boolean[][] board = new boolean[nrRows][nrColumns];
        for(int i = 0; i < nrRows; i++){
            for(int y = 0; y < nrColumns; y++){
                board[i][y] = false;
            }
        }
    }


    private void printBoard(){
        for (int j = board[0].length - 1; j >= 0; j--) { //java.lang.NullPointerException      


            System.out.print("\n " + (1 + j));
            for (int i = 0; i < board.length; i++) {
                System.out.print(board[i][j] ? " X" : " -");
            }
        }
        System.out.print("\n  ");
        for (int i = 1; i <= board.length; i++) {
            System.out.print(" " + i);
        }
        System.out.println("\n" + (whiteToMove ? white : black) + " ist am Zug.");
    }

public void startGame(){
    determineBoardSize();
    initBoard();
    determineFirstPlayer();
    printBoard();
    mainLoop();
    reportWinner();
}


public static void main(String[] args) {
    Dame ds = new Dame("Weiß", "Schwarz");
    ds.startGame();
}
  • 3
    The only thing you dereference on that line is `board`, so `board` must be `null`. Take a look at [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jorn Vernee Dec 09 '16 at 11:27
  • `boolean[][] board = new boolean[nrRows][nrColumns];` creates a local variable, so in `initBoard()` you are not working with the class atributte as in `printBoard()` – ttulka Dec 09 '16 at 11:30
  • The guys answered your question but just to help you some more, you should not use board[0] without having a NULL check, most of the times a NULL check is really a good thing. Before using board[0] do an `if(board !=null)` – Raul Cuth Dec 09 '16 at 11:33
  • I will change that. Thanks! – Philipp Dec 09 '16 at 11:39

2 Answers2

1

In initBoard you are initialising a new board and not the global board and therefore when the method returns the board is still uninitialised.

Aimee Borda
  • 842
  • 2
  • 11
  • 22
0
private void initBoard(){
    boolean[][] array = new boolean[nrRows][nrColumns]; 
    for(int i = 0; i < nrRows; i++){
        for(int y = 0; y < nrColumns; y++){
            array[i][y] = false;
        }
    } board = array;
}

Thanks...I didn't see that. Now I initialize the array board.