如何调试NullPointerException?

时间:2015-08-02 18:18:09

标签: java

我正在尝试将船放在基于控制台的棋盘游戏上,我有以下错误,不知道如何调试它们。

  

线程“main”java.lang.NullPointerException中的异常         at NavalBattle.Board.print(Board.java:570)         at NavalBattle.Board.placeShipsP1(Board.java:267)         在NavalBattle.Board。(Board.java:61)         at NavalBattle.Controller.start(Controller.java:29)         在NavalBattle.Controller.main(Controller.java:21)

第570行是value = this.ships2 [row] [col] .toString();

import java.util.Scanner;

  public class Controller {

    /**
     * The board that represents the current state of the game.
     */
    private Board board;

    public Controller() {}

    /**
     * This creates one Library object and calls its start method.
     * 
     * @Param args
     */
    public static void main(String[] args) {
        new Controller().start();
    }
    int PL;
    /**
     * The PLrimary method that starts and coordinates a game. Call this to
     * begin a new game.
     */
    public void start() {
        this.board = new Board();

        Scanner scanner = new Scanner(System.in);
        this.board.moveShip();
        while (!this.board.isGameOver()) {
            PL = 1;
            this.board.moveShip();
            PL = 2;
            this.board.moveShip2(null);
            this.board.remap();
            this.board.moveShip2(null);
        System.out.println("Enter a row and column number at which to shoot (e.g., 2,3): ");
        String[] coordinates = scanner.nextLine().split(",");
        if (coordinates.length != 2) {
        System.out.println("PLlease enter coordinates in the correct format.");
            continue;
            }
            int row = Integer.parseInt(coordinates[0].trim());
            int column = Integer.parseInt(coordinates[1].trim());
            this.board.bombOp(row, column);
        }
        if (PL == 1);{
        System.out.println("Game Over Player 2 wins");
        }
        if (PL == 2){
        System.out.println("Game Over Player 1 wins");  
        }
    }
}


// Board Class

import java.util.ArrayList;
import java.util.Scanner;

public class Board {

    int x = 0;
    int y = 0;

    ArrayList<Ships> player1 = new ArrayList<>();
    ArrayList<Ships> player2 = new ArrayList<>();
    /**
     * The number of shots that have hit ships
     */
    private int hitCount;

    /**
     * Track the locations of the board that have been fired upon
     */
    private boolean[][] locationsFiredUpon;

    /**
     * Track the ships in the game
     */
    private Ships[][] ships;

    private Ships[][] ships2;

    private Ships[][] shipsM;

    /**
     * The number of ships sunk
     */
    private int shipsSunk;

    /**
     * The number of shots fired
     */
    private int shotsFired;

    /**
     * Constructor. Initializes internal counters and populates the game
     * board with randomly placed ships.
     */
    public Board() {
        this.hitCount = 0;
        this.shipsSunk = 0;
        this.shotsFired = 0;
        board();
        this.ships = new Ships[6][9];
        this.ships2 = new Ships[6][9];
        this.locationsFiredUpon = new boolean[6][9];
        for (int row=0; row < this.ships.length; row++) {
            for (int col=0; col < this.ships[row].length; col++) {
                this.ships[row][col] = new Conflict();
                this.locationsFiredUpon[row][col] = false;
            }
        }

        this.placeShipsP1();
        this.placeShipsP2();
        this.moveShip();
        this.moveShip2(player1);
    }

    public void remap() {
        board();
        this.locationsFiredUpon = new boolean[6][9];
        for (int row=0; row < this.ships.length; row++) {
            for (int col=0; col < this.ships[row].length; col++) {
                this.ships[row][col] = new Conflict();
                this.locationsFiredUpon[row][col] = false;
            }
        }
    }

    /**
     * Constructor for testing. Accepts an input ship array and does not
     * attempt to place ships
     * @param shipArray
     */
    public Board(Ships[][] shipArray) {
        this.hitCount = 0;
        this.shipsSunk = 0;
        this.shotsFired = 0;
        this.ships = shipArray;
        this.locationsFiredUpon = new boolean[6][9];
        for (int row=0; row < this.ships.length; row++) {
            for (int col=0; col < this.ships[row].length; col++) {              
                this.locationsFiredUpon[row][col] = false;
            }
        }
    }

    public void board(){
        this.ships = new Ships[6][9];
        this.ships2 = new Ships[6][9];
    }

    /**
     * Get the number of shots fired that have hit a ship.
     * @return  The number of shots fired that have hit a ship
     */
    public int getHitCount() {
        return this.hitCount;
    }

    /**
     * Get the game board.
     * @return  A 2-D array of Ships representing the game
     */
    public Ships[][] getShipArray() {
        return this.ships;
    }

    public Ships[][] getShipArray2() {
        return this.ships2;
    }

    /**
     * Get the number of ships sunk.
     * @return  The number of ships sunk
     */
    public int getShipsSunk() {
        return this.shipsSunk;
    }

    /**
     * Get the number of shots fired so far.
     * @return  The number of shots fired
     */
    public int getShotsFired() {
        return this.shotsFired;
    }



    /**
     * Create an list of ships that can be placed on the board:
     * - 1 battleship
     * - 2 cruisers
     * - 3 destroyers
     * - 4 submarines
     * @return  ships to be placed on the board
     */
    private ArrayList<Ships> generateInitialShipArrayList1() {
        ArrayList<Ships> ship = new ArrayList<Ships>();

        for (int i=0; i<1; i++) {
            ship.add(new Battleship());
        }   
        for (int i=0; i<0; i++) {
            ship.add(new Minesweeper());
        }
        for (int i=0; i<0; i++) {
            ship.add(new Destroyer());
        }
        for (int i=0; i<0; i++) {
            ship.add(new Submarine());
        }   
        for (int i=0; i<0; i++) {
            ship.add(new Mines());
        }   
        return ship;
    }

    private ArrayList<Ships> generateInitialShipArrayList2() {
        ArrayList<Ships> ship2 = new ArrayList<Ships>();

        for (int i=1; i<2; i++) {
            ship2.add(new Battleship());
        }   
        for (int i=1; i<1; i++) {
            ship2.add(new Minesweeper());
        }
        for (int i=1; i<1; i++) {
            ship2.add(new Destroyer());
        }
        for (int i=1; i<1; i++) {
            ship2.add(new Submarine());
        }   
        for (int i=1; i<1; i++) {
            ship2.add(new Mines());
        }   
        return ship2;
    }

    /**
     * Indicate whether the game is over by checking if all ships have been 
     * sunk.
     * @return  true if all ships are sunk; otherwise false
     */
    public boolean isGameOver() {

        return (this.shipsSunk == 1);
    }

    /**
     * Whether the specified position is occupied by a ship and is not empty
     * sea.
     * @param row
     * @param column
     * @return true if the position has a ship; else false
     */
    public boolean isOccupied(int row, int column) {
        System.out.println("Occupardo, you no go here");
        return !(this.ships[row][column] instanceof Conflict);
    }

    public void placeShipsP1() {
        System.out.println("");
        print();

        ArrayList<Ships> shipsToMove = this.generateInitialShipArrayList1();
        player1 = this.generateInitialShipArrayList1();

        boolean okToPlaceShipHere = false;
        int row = 0;
        int column = 0;
        String Tp = "1";
        boolean horizontal = false;
        if(okToPlaceShipHere = false){
                System.out.println("Occupardo, you no go here");
            }
        for (Ships ship : player1) {
            while (!okToPlaceShipHere) {

                Scanner scanner = new Scanner( System.in );

                System.out.println("player1 enter a row and column (e.g., 2,3)");

                System.out.println("please enter the row of " + ship.getShipType());

                String[] coordinates = scanner.nextLine().split(",");
                if (coordinates.length != 2) {
                    System.out.println("Please enter coordinates in the correct format.");
                    continue;
                }
                row = Integer.parseInt(coordinates[0].trim());
                column = Integer.parseInt(coordinates[1].trim());
                ship.setX(row);
                ship.setY(column);
                System.out.println(ship.getX() + " " + ship.getY());

               // row = Random.nextInt(6);
               // column = Random.nextInt(9);
                horizontal = true;

                okToPlaceShipHere = ship.okToPlaceShipAt(row,  column, horizontal, this);

            }
            if( okToPlaceShipHere = true){
                this.bombOp(row, column);
            }

            ship.setBowColumn(column);
            ship.setBowRow(row);
            ship.setPlayer(Tp);
            ship.setX(row);
            ship.setY(column);
            System.out.println(ship.getX() + " " + ship.getY()  + " " + ship.getPlayer());
            ship.setHorizontal(horizontal);
            this.placeShipIntoShipsArray(ship);
            okToPlaceShipHere = false;
            System.out.println("");
            print();
        }       
    }

    public void placeShipsP2() {
        System.out.println("");
        print();

        ArrayList<Ships> shipsToPlace = this.generateInitialShipArrayList1();
        player2 = this.generateInitialShipArrayList2();

        boolean okToPlaceShipHere = false;
        int row = 0;
        int column = 0;
        String Tp = "2";
        boolean horizontal = false;
        if(okToPlaceShipHere = false){
                System.out.println("Occupardo, you no go here");
            }
        for (Ships ship : player2) {
            while (!okToPlaceShipHere) {

                Scanner scanner = new Scanner( System.in );

                System.out.println("Player 2 enter a row and column (e.g., 2,3)");

                System.out.println("please enter the row of " + ship.getShipType());

                String[] coordinates = scanner.nextLine().split(",");
                if (coordinates.length != 2) {
                    System.out.println("Please enter coordinates in the correct format.");
                    continue;
                }
                row = Integer.parseInt(coordinates[0].trim());
                column = Integer.parseInt(coordinates[1].trim());
                ship.setX(row);
                ship.setY(column);
                System.out.println(ship.getX() + " " + ship.getY());

//              row = random.nextInt(6);
//              column = random.nextInt(9);
                horizontal = true;

                okToPlaceShipHere = ship.okToPlaceShipAt(row,  column, horizontal, this);

            }


            ship.setBowColumn(column);
            ship.setBowRow(row);
            ship.setPlayer(Tp);
            ship.setX(row);
            ship.setY(column);
            System.out.println(ship.getX() + " " + ship.getY() + " " + ship.getPlayer());
            ship.setHorizontal(horizontal);
            this.placeShipIntoShipsArray(ship);
            okToPlaceShipHere = false;
            System.out.println("");
            print();
        }       
    }

    /**
     * Place the given ship into locations in the
     * 2Darray that tracks ship positions.
     * @param ship
     */
    private void placeShipIntoShipsArray(Ships ship) {
        int row = ship.getBowRow();
        int column = ship.getBowColumn();

//      player1.add.ship;
        player1.removeAll(player1);
        player2.removeAll(player2);

        if (ship.isHorizontal()) {
            for (int i=0; i < ship.getLength(); i++) {              
                this.ships[row][(column+i)] = ship;
            }
        }
        else {
            for (int i=0; i < ship.getLength(); i++) {              
                this.ships[(row+i)][column] = ship;
            }
        }
    }

    public void ifship(){
        for (int i=0; i < 1; i++){
            board();
        }

    }

    private void placeShipIntoShipsArray2(Ships ship) {
        int row = ship.getBowRow();
        int column = ship.getBowColumn();


        //player1.add.ships;
        player1.removeAll(player1);
        player2.removeAll(player2);
        //ship.clear();

        if (ship.isHorizontal()) {
            for (int i=0; i < ship.getLength(); i++) {              
                this.ships2[row][(column+i)] = ship;
            }
        }
        else {
            for (int i=0; i < ship.getLength(); i++) {              
                this.ships2[(row+i)][column] = ship;
            }
        }

    }
public void display() {

        System.out.print("    ");
        for (int i=0; i < 9; i++) {
            System.out.print(i + "  ");
        }
        System.out.print("\n");

        for (int row=0; row < 6; row++) {
            System.out.print(" " + row + "  ");
            for (int col=0; col < 9; col++) {
                String value = "";
                if (this.locationsFiredUpon[row][col]) {
                    value = "-";
                }
                if (this.ships2[row][col] != null) {
                    value = this.ships2[row][col].toString() ;
                }
                else {
                    value = "-";
                }

                System.out.print(value + "  ");
            }
            System.out.print("\n");
        }
        remap();
    } 


    boolean pr = false;
    boolean pr2 = false;

    public void moveShip(){
        System.out.println("So you wanna move a ship");
        System.out.println(player1);

        for (int i=0; i > 1; i++) {
            print();
            System.out.println("map 1");
            pr = true;
        }
        if (pr = true){
            display();
        }

        //ArrayList<Ships> shipsToMove = new ArrayList<>();
        ArrayList<Ships> shipsToMove = new ArrayList<>();
        ArrayList<Ships> shipsToMove1 = player1;
        boolean notMoved = false;
        int rowM = 0;
        int columnM = 0;
        String Tp = "1";
        boolean horizontal = false;
        if(notMoved = false){
                System.out.println("Occupardo, you no go here");
            }
        System.out.println("gets here 1");

        for (Ships shipM : shipsToMove1) {
            System.out.println("gets here 2");
            while (!notMoved) {


                Scanner scanner1 = new Scanner( System.in );

                System.out.println("Enter a area to move to (e.g., 2,3)");

                System.out.println("please enter a movement for the: " + shipM.getShipType() + shipM.getBowRow());

                String[] coordinates = scanner1.nextLine().split(",");
                if (coordinates.length != 2) {
                    System.out.println("Please enter coordinates in the correct format.");
                    continue;
                }
                rowM = Integer.parseInt(coordinates[0].trim());
                columnM = Integer.parseInt(coordinates[1].trim());

//              row = random.nextInt(6);
//              column = random.nextInt(9);
                horizontal = true;

                notMoved = shipM.okToPlaceShipAt(rowM,  columnM, horizontal, this);             
            }

            if( notMoved = true){
                this.bombOp(rowM, columnM);
            }

            shipM.setBowColumn(columnM);
            shipM.setBowRow(rowM);
            shipM.setHorizontal(horizontal);
            this.placeShipIntoShipsArray2(shipM);
            notMoved = false;
            System.out.println("");
            display();
        }       
    }

    public void moveShip2(ArrayList<Ships> player2){
        System.out.println("So you wanna move a ship");

        for (int i=0; i > 1; i++) {
            display();
            pr2 = true;
        }
        if (pr2 = true){
            display();
        }

        ArrayList<Ships> shipsToMove = new ArrayList<>();
        player2 = this.player2;
        boolean notMoved = false;
        int rowM = 0;
        int columnM = 0;
        String Tp = "1";
        boolean horizontal = false;
        if(notMoved = false){
                System.out.println("Occupardo, you no go here");
            }
        System.out.println("gets here 1");

        for (Ships ship : player2) {
            System.out.println("gets here 2");
            while (!notMoved) {

                Scanner scanner1 = new Scanner( System.in );

                System.out.println("Enter a area to move to (e.g., 2,3)");

                System.out.println("please enter a movement for the: " + ship.getShipType());

                String[] coordinates = scanner1.nextLine().split(",");
                if (coordinates.length != 2) {
                    System.out.println("Please enter coordinates in the correct format.");
                    continue;
                }
                rowM = Integer.parseInt(coordinates[0].trim());
                columnM = Integer.parseInt(coordinates[1].trim());

//              row = random.nextInt(6);
//              column = random.nextInt(9);
                horizontal = true;

                notMoved = ship.okToPlaceShipAt(rowM,  columnM, horizontal, this);              
            }
            if( notMoved = true){
                this.bombOp(rowM, columnM);
            }

            ship.setBowColumn(columnM);
            ship.setBowRow(rowM);
            ship.setHorizontal(horizontal);
            this.placeShipIntoShipsArray2(ship);
            notMoved = false;
            System.out.println("");
            display();
            display();
        }       
    }




    /**
     * Print the current state of the game.
     */
    public void print() {

        System.out.print("    ");
        for (int i=0; i < 9; i++) {
            System.out.print(i + "  ");
        }
        System.out.print("\n");

        for (int row=0; row < 6; row++) {
            System.out.print(" " + row + "  ");
            for (int col=0; col < 9; col++) {
                String value = "";
                if (this.locationsFiredUpon[row][col]) {
                    value = "-";
                }
                **if (this.ships[row][col] != null) {
                    value = this.ships[row][col].toString() ;**
                }

                if (this.locationsFiredUpon[row][col]) {
                    value = this.ships2[row][col].toString();
                }
                if (this.ships2[row][col] != null) {
                value = this.ships2[row][col].toString() ;
                }
                else {
                    value = "-";
                }

                System.out.print(value + "  ");
            }
            System.out.print("\n");
        }

    } 


    public void print2() {

        System.out.print("    ");
        for (int i=0; i < 9; i++) {
            System.out.print(i + "  ");
        }
        System.out.print("\n");

        for (int row=0; row < 6; row++) {
            System.out.print(" " + row + "  ");
            for (int col=0; col < 9; col++) {
                String value = "";
                if (this.locationsFiredUpon[row][col]) {
                    value = "-";
                }
                if (this.shipsM[row][col] != null) {
                    value = this.shipsM[row][col].toString() ;
                }
                if (this.locationsFiredUpon[row][col]) {
                value = this.ships2[row][col].toString();
            }
                if (this.ships2[row][col] != null) {
                value = this.ships2[row][col].toString() ;
            }
                else {
                    value = "-";
                }

                System.out.print(value + "  ");
            }
            System.out.print("\n");
        }
        board();
    }
    /**
     * Handle a shot fired at the specified position.
     * @param row
     * @param column
     * @return  true if a ship was hit; else false
     */
    public boolean bombOp(int row, int column) {
        this.shotsFired++;
        this.locationsFiredUpon[row][column] = true;

        Ships shipAtLocation = this.ships[row][column];     

        if (shipAtLocation.isSunk()) {
            return false;
        }

        boolean shipWasHit = shipAtLocation.bombOp(row, column);

        if (shipWasHit) {
            this.hitCount++;
        }

        if (shipAtLocation instanceof Conflict) {
            return shipWasHit;
        }

        if (shipAtLocation.isSunk()) {
            shipsSunk++;
        }

        return shipWasHit;
    }

1 个答案:

答案 0 :(得分:4)

调试NullPointerException的最佳方法是查看堆栈跟踪的第一行,即:

NavalBattle.Board.print(Board.java:570)

这意味着错误发生在Board.java文件中的第570行。您应该转到文件中的这一行,并查看该行上哪些变量为空。

第570行是value = this.ships2[row][col].toString();。在这一行上,只有3件事可能为空:

  1. this.ships2
  2. `this.ships2 [行]
  3. `this.ships2 [行] [山口]
  4. 如果您无法立即看到哪些是null,请使用IDE在那里设置断点并以调试模式启动程序。当IDE停在该行时,为上述3个表达式中的每个表达式创建一个“监视表达式”,或使用“计算表达式”功能。其中一个应为null。

    没有调试,我只能猜测,但我想我可以做出很好的猜测。 this.ship2不太可能为null,因为我可以看到您已在代码中初始化它。我也不认为this.ships2 [row]为null - 我认为你在构造函数中正确初始化了2D数组。因此,我认为this.ships2 [row] [col]为null,并且在null上调用toString()会抛出NPE。为了证实我的理论,您可以用以下代码替换该行:

    value = String.valueOf(this.ships2[row][col]);