I have initialized the board and I am trying to print it but it always gives me null pointer exception. This is my Checkers.java class:
package specialCheckers;
/*There is another class:
Checkers which contains a Cell[][] board and a String message.
Checkers should have mutators and accessors for these.*/
public class Checkers {
Cell[][] board;
String message;
/**********Setters and getters Started*****/
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;
}
/**********Setters and getters Ended*****/
/*Checkers should also have a public void init() method that sets up the board as you see it in the video below:
White cells on the top three rows.
The first row starts with an empty cell, then a white, then empty, etc.
The second row has that order reversed and the third row is identical to the first one; Red chips go in the bottom 3 rows.
The last row starts with a black chip, then empty, then black, etc.
The second to last has that order reversed, and the third to last is identical to the last one.*/
/*********initialization******/
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;
}
/**
* Creates empty rows
* . . . . . . . .
*
* @param row
* @return
*/
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;
}
/**
* Creates row of Black & Empty cells
* B . B . B . B ,
* @param row
* @return
*/
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;
}
/*
* Creates row of Empty & White
* . W . W . W . W .
*/
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;
}
/*
* Creates row of White & Empty
* W . W . W . W .
*/
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;
}
/**
* Creates row of Black & Empty cells
* . B . B . B . B
* @param row
* @return DE
*/
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;
}
/*********initialization ended*******/
/*Checkers has a method public void printBoard()
which will print the board with the indices of the columns at the top
and the indices of the rows at the left. (See video for reference).*/
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 is here it is the parent class of 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 it give 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 on what line 167 is its the 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();
}
}
error is on System.out.print(cell.toString());