为什么我在战舰游戏中遇到错误java.lang.NullPointerException?

时间:2014-11-26 04:39:40

标签: java arrays class object nullpointerexception

我正在尝试对战舰游戏进行编码,并在运行时遇到奇怪的错误:线程“main”中的异常java.lang.NullPointerException 在Caculations.findShip(Caculations.java:29)     在Board.main(Board.java:60)

请帮助我卡住,我不知道如何继续!这是我的代码:(注意,它在我的eclipse工作环境中的2个类文件中)

public class Board {

    public static void main(String[] args) {

        boolean continuePlay = true;
        int[][] board = new int[10][10]; // creating 2d array 'board'
        char[][] boardGraphical = new char[10][10]; // creating 2d array 'board
                                                    // this time the visual
        for (int x = 0; x < 10; x++) { // for within for //initializing elements
                                        // in both boards using double for
                                        // method
            for (int y = 0; y < 10; y++) {
                board[x][y] = 0;
                boardGraphical[x][y] = 'o';
                System.out.println("Board element " + x + " " + y // printing
                                                                    // initialized
                                                                    // elements
                                                                    // here
                        + " initialized");
            }

        }

        /*
         * 1) Make user ships 1 and computer ships 2 (all numbers other than 0 =
         * true)
         * 
         * 2) Make it where if the computer gets a hit on a '1' than it sets
         * that value to like a 3 or something so it knows when the ship is
         * sunk. So in a if it does if([x][y] && [x][y])
         * System.out.println("You sunk my ship!");
         * 
         * 3) REMEMBER YOU CAN DO MULTIPLE IFS INSIDE IFS FOR MULTIPLE
         * CONDITIONS. 4) declare ships here!
         * 
         * 5) PUT STUFF IN A WHILE LOOP SO COMP CAN KEEP GOING
         */
        board[3][3] = 1; // declaring a battleship. Very important.
        board[3][4] = 1;
        board[3][5] = 1;
        boardGraphical[3][3] = 's';
        boardGraphical[3][4] = 's';
        boardGraphical[3][5] = 's';

        while (continuePlay == true) { // while loop so that computer keeps
                                        // guessing
            // WITHIN THIS LOOP KEEP REPRINTING THE BOARD
            double computerChoiceXd = Math.floor(Math.random() * 10); // using
                                                                        // Math.random
                                                                        // functions
                                                                        // for
                                                                        // computers
                                                                        // first
                                                                        // guess
                                                                        // to be
                                                                        // a
                                                                        // random
                                                                        // num
            double computerChoiceYd = Math.floor(Math.random() * 10);
            int computerChoiceX = (int) computerChoiceXd;
            int computerChoiceY = (int) computerChoiceYd;

            if (board[computerChoiceX][computerChoiceY] == 1) { // checking if
                                                                // math.random
                                                                // landed on a
                                                                // ship point
                System.out.println("Computer got a hit at " + computerChoiceX
                        + " " + computerChoiceY);
                board[computerChoiceX][computerChoiceY] = 2; // setting the
                                                                // point as 2 or
                                                                // 'hit'
                boardGraphical[computerChoiceX][computerChoiceY] = 'H';
                for (int row = 0; row < 10; row++) { // printing out graphical
                                                        // board using the same
                                                        // method as when
                                                        // intializing
                    for (int col = 0; col < 10; col++) {
                        System.out.print(boardGraphical[row][col]);
                    }
                    System.out.println(" "); // spacer for printing
                }

                Caculations test = new Caculations(computerChoiceX, // Creating
                                                                    // a new
                                                                    // object of
                                                                    // calculation
                        computerChoiceY, board);
                test.findShip();
                // break;

                if (board[3][3] == 2) { // checking to see if ship is sunk using
                                        // a triple if statement
                    if (board[3][4] == 2) {
                        if (board[3][5] == 2) {
                            System.out.println("Battleship sunk!");

                            continuePlay = false; // if so than it breaks out of
                                                    // loop to end the game
                            break;
                        }
                    }

                }
            } else if (board[computerChoiceX][computerChoiceY] == 0) { // otherwise
                                                                        // if
                                                                        // the
                                                                        // area
                                                                        // is a
                                                                        // 0 or
                                                                        // 'unmarked'
                System.out.println("Computer missed  at " + computerChoiceX
                        + " " + computerChoiceY);
                boardGraphical[computerChoiceX][computerChoiceY] = 'x'; // mark
                                                                        // area
                                                                        // as a
                                                                        // miss
                for (int row = 0; row < 10; row++) { // print out board
                    for (int col = 0; col < 10; col++) {
                        System.out.print(boardGraphical[row][col]);
                    }
                    System.out.println(" ");
                }

            }

        }

    }

}





public class Caculations {
    int xValue;
    int yValue;
    int[][] myArray;
    int[][] storage = new int[10][10];
    boolean xAxisChangeP;
    boolean yAxisChangeP;
    boolean xAxisChangeN;
    boolean yAxisChangeN;
    boolean notSunk;

    Caculations(int x, int y, int[][] myArray) {
        xValue = x;
        yValue = y;
        xAxisChangeP = true;
        yAxisChangeP = true;
        xAxisChangeN = true;
        yAxisChangeN = true;
        notSunk = true;

    }

    void findShip() {
        while (notSunk == true) {
            // 1
            while (xAxisChangeP == true) {
                if (myArray[xValue + 1][yValue] == 1) {
                    myArray[xValue + 1][yValue] = 2;
                    if (myArray[3][3] == 2) {
                        if (myArray[3][4] == 2) {
                            if (myArray[3][5] == 2) {
                                System.out.println("Battleship sunk!");
                                notSunk = false;

                            }
                        }
                    }
                    continue;
                }

                else {
                    xAxisChangeP = false;

                }

            }
            while (xAxisChangeN == true) {
                if (myArray[xValue - 1][yValue] == 1) {
                    myArray[xValue - 1][yValue] = 2;
                    if (myArray[3][3] == 2) {
                        if (myArray[3][4] == 2) {
                            if (myArray[3][5] == 2) {
                                System.out.println("Battleship sunk!");
                                notSunk = false;

                            }
                        }
                    }
                    continue;
                }

                else {
                    xAxisChangeN = false;
                }
            }

            // 1
            while (yAxisChangeP == true) {
                if (myArray[xValue][yValue + 1] == 1) {
                    myArray[xValue][yValue + 1] = 2;
                    if (myArray[3][3] == 2) {
                        if (myArray[3][4] == 2) {
                            if (myArray[3][5] == 2) {
                                System.out.println("Battleship sunk!");
                                notSunk = false;

                            }
                        }
                    }
                    continue;
                }

                else {
                    yAxisChangeP = false;

                }

            }
            while (yAxisChangeN == true) {
                if (myArray[xValue][yValue - 1] == 1) {
                    myArray[xValue][yValue - 1] = 2;
                    if (myArray[3][3] == 2) {
                        if (myArray[3][4] == 2) {
                            if (myArray[3][5] == 2) {
                                System.out.println("Battleship sunk!");
                                notSunk = false;

                            }
                        }
                    }
                    continue;
                }

                else {
                    yAxisChangeN = false;
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:3)

你有初始化数组吗? Best是调试您的代码以查看,哪个语句抛出异常。在eclipse中,您可以添加Null Pointer Exception作为断点和调试。

答案 1 :(得分:2)

你使用myArray,但你永远不会初始化它。

public class Caculations {
   int xValue;
   int yValue;
   int[][] myArray;  // array declared but never initialized

   // ....

   void findShip() {
      while (notSunk == true) {
         // 1
         while (xAxisChangeP == true) {
            if (myArray[xValue + 1][yValue] == 1)  // then you use it here

解决方案:在使用前初始化变量。

更重要的是,您需要学习如何调试NPE(NullPointerException)的一般概念。 你应该批判性地阅读你的异常堆栈跟踪以找到错误的代码行,抛出异常的行,然后仔细检查该行,找出哪个变量为null,然后追溯到你的代码,看看为什么。你会一次又一次地碰到这些,相信我。

答案 2 :(得分:0)

Calculations的构造函数中,您从未初始化myArray

Caculations(int x, int y, int[][] myArray) {
    xValue = x;
    yValue = y;
    xAxisChangeP = true;
    yAxisChangeP = true;
    xAxisChangeN = true;
    yAxisChangeN = true;
    notSunk = true;
    this.myArray = myArray; //Add this line
}

这是对您的问题的直接回答,但总而言之,您应该对所引发的异常背后的含义进行一些研究,以便您理解它的含义。


为什么会出现这个问题

在Java中,所有对象和基元(如果未手动初始化)都将被赋予默认值。 对于基元的默认值,请选中:Primitive Data Types

如果是非基本类型 - 例如ObjectStringThread等,以及任何用户定义的类(即Calculations)和也是数组(即myArray) - 默认值为null

考虑到这一点,在构造函数中,如上所示,您尚未初始化myArray,这意味着当第一次访问此变量时,返回的值为null。< / p>


那么,null的问题是什么?

嗯,就其本身而言,它没有任何伤害。它就在那里。它不会打扰你。直到你决定使用一个没有分配对象的变量,但不知怎的,你会忘记它,并把它当作像String或数组那样的东西对待。

当Java告诉你的时候:&#34;嘿!这里没有任何对象。我不能这样工作。让我们抛出异常!&#34;。