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

标签 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;
                }
            }
        }
    }
}

最佳答案

你初始化了myarray吗?最好是调试代码以查看哪个语句引发异常。在 Eclipse 中,您可以添加 NullPointerExeption 作为断点并进行调试。

关于java - 为什么我在战舰游戏中收到错误 java.lang.NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141455/

相关文章:

java - 如果银行账户类别中遗漏了报表?

arrays - Bash 自动完成 - 如何将此数组传递给 compgen 而不会折叠明显的空格?

javascript - 无法通过 AJAX 将多维数组从 JavaScript 发送到 PHP

java - 如何从Android中的其他类而不是Activity调用Activity中的方法?

php - 使用 BulletProof 上传类时在上传前调整图像大小

java - 如何监听 OutOfMemoryError 并退出 JVM

java - JUnit 5 TestSuite 替代方案?

javascript - 如何从原型(prototype) "class"中调用原型(prototype)函数?

Java Swing 接口(interface)问题

c# - 以最佳方式检查算术字符串括号对称性