2015-08-02 2 views
0

Я пытаюсь разместить корабли на консольной настольной игре, у меня есть следующие ошибки и не знаю, как их отладить.Как отлаживать исключение NullPointerException?

Исключение в потоке "основного" java.lang.NullPointerException в NavalBattle.Board.print (Board.java:570) на NavalBattle.Board.placeShipsP1 (Board.java:267) в NavalBattle.Board . (Board.java:61) в NavalBattle.Controller.start (Controller.java:29) на NavalBattle.Controller.main (Controller.java:21)

линии 570 значение = this.ships2 [строка] [столбец] .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

Вы должны рассмотреть вопрос об изменении заголовка этого вопроса на «Как мне отладить исключение NullPointerException?». «Консольная настольная игра» на самом деле ничего не говорит о вашей проблеме. – user1886323

+0

Предлагаю вам прочитать: http://stackoverflow.com/help/how-to-ask немного более тщательно. «Включите достаточно кода, чтобы другие могли воспроизвести проблему. Для получения справки прочтите« Как создать минимальный, полный и проверенный пример. »« В частности, эта часть. Ваш вопрос довольно ясен, но есть слишком много кода для прохождения. Наконец, когда вы получили эту работу, я предлагаю вам дать http://codereview.stackexchange.com/ все, что вам нужно. – Emz

+0

У вас также есть ';' в неправильном месте вашего 'Controller'. Не связано с ошибкой, которую вы получаете. 'if (PL == 1); {'. Я думаю, это должно быть без этого. Вы также можете использовать случай else вместо проверки 'PL == 2'. Также есть ошибки в том, как вы переключаетесь между «1» и «2». – Emz

ответ

4

Лучший способ отладки NullPointerException, чтобы посмотреть на первой строке трассировки стека, что:

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

Что это означает, что ошибка произошло в строке 570 в файле Board.java. Вы должны перейти к этой строке в файле и посмотреть, какие переменные могут быть нулевыми по этой строке.

Линия 570 - value = this.ships2[row][col].toString();. На этой линии, только 3 вещи могут возможно быть пустым:

  1. this.ships2
  2. `this.ships2 [строка]
  3. ` this.ships2 [строка] [Col]

Если вы не может сразу увидеть, какая из них равна нулю, используйте вашу среду IDE для установки там точки останова и запуска вашей программы в режиме отладки. Когда среда IDE останавливается в этой строке, создайте «выражение часов» для каждого из трех выражений или используйте функцию «выражение оценки». Один из них должен быть нулевым.

Без отладки, я могу только догадываться, но я думаю, что могу сделать хорошее предположение. this.ship2 вряд ли будет нулевым, потому что я вижу, что вы его инициализировали в своем коде. Я также не думаю, что this.ships2 [row] имеет значение null. Вы правильно инициализировали 2D-массив в конструкторе. Поэтому я думаю, что это this.ships2 [row] [col], которое является нулевым, а вызов toString() на null - это выброс NPE. Чтобы подтвердить свою теорию, вы можете заменить линию:

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

Это моя строка 570: if (this.locationFiredUpon [row] [col]) { \t \t \t \t \t value = this.ships2 [row] [col] .toString(); \t \t \t \t} –

+0

Я не могу найти нулевую переменную, поэтому вам нужна помощь. –

+0

Вы должны добавить строку на свой вопрос, а не как комментарий. Но вот подсказка - поместите новую строку перед значением, а затем номер строки в трассе стека может быть 570 или 571 - это сужает возможные места, где может быть значение null. – user1886323

Смежные вопросы