测试2D对象数组中的null

时间:2018-12-02 04:47:16

标签: java arrays multidimensional-array

我正在上一个必须创建游戏的课堂项目。我有两个类,BoardButton和TreasureButton,它们创建按钮。 TreasureButton类扩展了BoardButton类。

在我正在研究的类中,我试图创建一个包含2D数组(10x10)的面板,其中包含20个随机选择的TreasureButton类实例,而其余的80个BoardButton类实例。运行程序时,出现运行时错误:

java.lang.ArrayIndexOutOfBoundsException: -1157793070
    at GameBoardPanel.<init>(GameBoardPanel.java:46)
    at TreasureGame.<init>(TreasureGame.java:22)
    at TreasureGame.main(TreasureGame.java:33)

引发错误的行上的代码是:

if (gameBoardButtons[row][col] == null)

由于我尚未初始化数组,因此我认为应将数组选定索引处的值设置为null。任何帮助将不胜感激。

public class GameBoardPanel extends JPanel
{
    private BoardButton[][] gameBoardButtons;       // BoardButton 2D array that will be used to populate the board
    private final int NUM_TREASURE_BUTTONS = 20;    // Number of treasure buttons that will be added to the array
    private final int NUM_ROWS = 10;                // Number of rows in the array
    private final int NUM_COLS = 10;                // Number of columns in the array

    public GameBoardPanel()
    {
        int treasureButtonInstances = 0;            // Used to count the number of TreasureButton instances

        // Create the 'gameBoardButtons' array and make it a 10x10 array
        gameBoardButtons = new BoardButton[NUM_ROWS][NUM_COLS];

        // Build an object from the Random class that chooses a number between 0-9
        Random randomNumber = new Random(10);

        // Build a while loop that randomly adds 20 different TreasureButton instances to the 'gameBoardButtons' BoardButton array
        while (treasureButtonInstances < NUM_TREASURE_BUTTONS)
        {
            // Obtain two random numbers that will be used to assign a TreasureButton instance to the 'gameBoardButtons' BoardButton array
            int row = randomNumber.nextInt();
            int col = randomNumber.nextInt();

            // If statement that adds an instance of the TreasureButton class if that particular row/column of the 'gameBoardButtons' BoardButton array is empty
            if (gameBoardButtons[row][col] == null)
            {
                // Create an instance of the TreasureButton class and assign it to the particular row/col of our 'gameBoardButtons' BoardButton array
                gameBoardButtons[row][col] = new TreasureButton();

                // Increment the 'treasureButtonInstances' variable, as an instance of the TreasureButton class has been created
                treasureButtonInstances++;
            }// End of the if statement
        }// End of the while loop

        // Build a nested for loop that will populate the rest of the 'gameBoardButtons' BoardButton array
        for (int row = 0; row < NUM_ROWS; row++)
        {
            for (int col = 0; row < NUM_COLS; row++)
            {
                // If statement that will assign an instance of the BoardButton class if that particular row/col of our 'gameBoardButtons" BoardButton array is empty
                if (gameBoardButtons[row][col] == null)
                {
                    // Create an instance of the BoardButton class and assign it to the particular row/col of our 'gameBoardButtons' BoardButton array
                    gameBoardButtons[row][col] = new BoardButton();
                }// End of the if statement
            }//End of the nested for loop
        }// End of the for loop
    }// End of the GameBoardPanel no-arg constructor

2 个答案:

答案 0 :(得分:1)

您未指定BaseEntity<Id>的上限。

来自javadoc

  

使用一个长种子创建一个新的随机数生成器。种子是由方法next(int)维护的伪随机数生成器内部状态的初始值。

Javadoc of nextInt()强调我的

  

从该随机数生成器的序列返回下一个伪随机,均匀分布的int值。 nextInt的一般约定是一个int值是伪随机生成并返回的。 所有2 32 个可能的int值(大约)相等的概率产生。

这就是您的"Id"的原因。

您需要使用带有上限的overloaded method of nextInt

new Random(10)

另一个问题(如other answer所述)-您弄错了 for 循环计数器变量名

ArrayIndexOutOfBoundsException

必须

int row = randomNumber.nextInt(NUM_ROWS);
int col = randomNumber.nextInt(NUM_COLS);

答案 1 :(得分:0)

Random的构造函数中的参数是种子,它可以防止每次生成相同的序列,但与随机整数的范围无关。

在大多数情况下,您不必设置种子,只需使用new Random(),Java就会自行处理种子。

要将生成的数字限制为0到9,应调用randomNumber.nextInt(10)而不是randomNumber.nextInt()