打印检查板时出现空指针异常?

时间:2013-08-06 22:11:45

标签: java

我已初始化了主板,我正在尝试打印它,但它总是给我空指针异常。这是我的Checkers.java类:

 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();
        }
    }


而我的Cell Class在这里它是黑白类的父类,返回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;
}
}

这是我的主要方法。

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();
        }
    }
}

它给出的错误如下:

Exception in thread "main" java.lang.NullPointerException
    at specialCheckers.Checkers.printBoard(Checkers.java:167)
    at specialCheckers.TestCheckers.main(TestCheckers.java:9)

添加第167行是printBoard方法

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();
    }
}

错误在System.out.print(cell.toString());

2 个答案:

答案 0 :(得分:5)

而不是做

    board = new Cell[8][8];


    Cell[][] a = { ... };

DO

    Cell[][] a = { ... };

    board = a;

问题是您正在构建一个电路板,但从未将其分配给board变量,因此您最终会在null中得到一个8x8的board值网格。

答案 1 :(得分:1)

你可以这样做

   board =new Cell[8][8];//remove this line
   Cell[][] a = { ... }
   //just assign it to your board
   board = a;