4

ボードを初期化して印刷しようとしていますが、常に null ポインター例外が発生します。これは私の 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();
        }
    }


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

4

2 に答える 2

5

する代わりに

    board = new Cell[8][8];


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

行う

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

    board = a;

board問題は、ボードを作成しているが、それを変数に割り当てていないためnullboard.

于 2013-08-06T22:24:28.700 に答える
1

あなたはこれを行うことができます

   board =new Cell[8][8];//remove this line
   Cell[][] a = { ... }
   //just assign it to your board
   board = a;
于 2013-08-06T22:37:49.483 に答える