I initialized the board, and I try to print it, but it always gives me an exception with an exception pointer. This is my Checkers.java class:
package specialCheckers; public class Checkers { Cell[][] board; String message; public void setBoard(Cell[][] board){ this.board = board; } public Cell[][] getBoard(){ return board; } public void setMessage(String message){ this.message = message; } public String getMessage(){ return message; } public void init(){ board = new Cell[8][8]; Cell[][] a = { getEmptyWhiteBlack("EW",0), getEmptyWhiteBlack("WE",1), getEmptyWhiteBlack("EW",2), getEmptyWhiteBlack("EE",3), getEmptyWhiteBlack("EE",4), getEmptyWhiteBlack("BE",5), getEmptyWhiteBlack("EB",6), getEmptyWhiteBlack("BE",7) }; } private Cell[] getEmptyWhiteBlack(String rowIndicator, int row){ Cell[] cellArray = new Cell[8]; if(rowIndicator.equals("EW")) cellArray = getEmptyWhite(row); else if(rowIndicator.equals("WE")) cellArray = getWhiteEmpty(row); else if(rowIndicator.equals("EE")) cellArray = getEmpty(row); else if(rowIndicator.equals("BE")) cellArray = getBlackEmpty(row); else if(rowIndicator.equals("EB")) cellArray = getEmptyBlack(row); return cellArray; } private Cell[] getEmpty(int row){ Cell[] cellArray = new Cell[8]; for(int i=0;i<8;i++){ Cell cell = new Cell(row,i); cellArray[i] = cell; } return cellArray; } private Cell[] getBlackEmpty(int row){ Cell[] cellArray = new Cell[8]; for(int i = 0; i<8; i++){ Cell cell = new Black(row,i); cellArray[i]=cell; } return cellArray; } private Cell[] getEmptyWhite(int row){ Cell[] cellArray = new Cell[8]; for(int i = 0; i<8; i++){ Cell cell; if(i%2 == 0){ cell = new Cell(row,i); } else{ cell = new White(row,i); } cellArray[i]=cell; } return cellArray; } private Cell[] getWhiteEmpty(int row){ Cell[] cellArray = new Cell[8]; for(int i = 0; i<8; i++){ Cell cell; if(i%2 == 0){ cell = new White(row,i); } else{ cell = new Cell(row,i); } cellArray[i]=cell; } return cellArray; } private Cell[] getEmptyBlack(int row){ Cell[] cellArray = new Cell[8]; for(int i = 0; i<8; i++){ Cell cell; if(i%2 == 0){ cell = new Cell(row, i); } else { cell = new Black(row,i); } cellArray[i]=cell; } return cellArray; } public void printBoard(){ for(int i = 0; i< 8; i++){ for(int j = 0; j< 8; j++){ Cell cell = board[i][j]; System.out.print(cell.toString()); } System.out.println(); } }
And My Cell Class here is the parent class of the black and white classes, which returns B, W:
public class Cell { public static final String EMPTY="."; int i; int j; String value; public Cell(int i, int j){ this.i = i; this.j = j; value = EMPTY; } public String toString(){ return value; } }
Here is my main method.
package specialCheckers; import java.util.Scanner; public class TestCheckers { public static void main(String [] args){ Checkers c = new Checkers(); c.init(); c.printBoard(); int[] chips = c.count(); Scanner kbd = new Scanner(System.in); while(chips[0]>0 && chips[1]>0){ System.out.println("\nYour move? 4 ints: src row, src col, dest row, dest col separated by [SPACE]"); int srcR = kbd.nextInt(); int srcC = kbd.nextInt(); int destR = kbd.nextInt(); int destC = kbd.nextInt(); kbd.nextLine(); c.move(srcR,srcC,destR,destC); c.printBoard(); System.out.println(c.getMessage()); c.count(); } } }
The error he gives is as follows:
Exception in thread "main" java.lang.NullPointerException at specialCheckers.Checkers.printBoard(Checkers.java:167) at specialCheckers.TestCheckers.main(TestCheckers.java:9)
Just to add which line 167 is its printBoard method
public void printBoard(){ for(int i = 0; i< 8; i++){ for(int j = 0; j< 8; j++){ Cell cell = board[i][j]; System.out.print(cell.toString()); } System.out.println(); } }
the error is on System.out.print(cell.toString());