通过访问JButton Array中的Object来获取NullPointerException

时间:2014-12-08 22:14:01

标签: java swing nullpointerexception

所以我创造了一个小蛇游戏。 在我的构造函数中,我称之为" CreateSnake" function,用JButtons创建一个Snake。

    private void createSnake()
    {
        snakeButtonx[0] = 100; //Sets the starter position of the snake
        snakeButtony[0] = 150;
        JButton tempButton; //temporary Button for adding button to the Snake

        // Initially the snake has length of 5
        for (int i = 0; i < sizeSnake; i++) 
        {
            snakeButton[i] = new JButton("" + i);
            snakeButton[i].setEnabled(false); // Disable the buttons so you cant click it

            tempButton = snakeButton[i];
            Surface.addSnake(tempButton, i);
            snakeButton[i].setBounds(snakeButtonx[i], snakeButtony[i], 10, 10);
            snakeButtonx[i + 1] = snakeButtonx[i] - 10;
            snakeButtony[i + 1] = snakeButtony[i];
        }
    }

然后我有一个移动函数,它通过一个线程

每100毫秒调用一次
@SuppressWarnings("deprecation")
private void move() 
{
    snakeButtonx[0] += directionx; //Set the position of the head of the snake
    snakeButtony[0] += directiony;

    for (int i = 0; i < sizeSnake; i++)
    {
        snakeButtonPos[i] = snakeButton[i].getLocation(); //Fills the snakeButtonPos integer with the positions of every Button
    }

    snakeButton[0].setBounds(snakeButtonx[0], snakeButtony[0], 10, 10);

    for (int i = 1; i < sizeSnake; i++) 
    {
        snakeButton[i].setLocation(snakeButtonPos[i - 1]);
    }
    show();
}

因此。到这里,它工作正常。但每当我想在我的JButton数组中添加第六个JButton时,我的移动方法中就会出现一个nullpointer异常。

private void Grow()
{
    JButton tempButton = new JButton(); 
    int newIndex = sizeSnake + 1;

    // Add the new Button to the Button Array
    snakeButton[newIndex] = new JButton();
    snakeButton[newIndex].setEnabled(false);

    tempButton = snakeButton[newIndex];
    Surface.addSnake(tempButton, newIndex);

    // Position is irrelevant, cause the position will be fixed at the next call of move()
    snakeButton[newIndex].setBounds(200, 300, 10, 10);

    sizeSnake = sizeSnake + 1;
}

第六个JButton在Jbutton数组中,而Im在我的Grow()函数中。但是在move()的下一次调用中,不再有一个索引为6的JButton。

为什么会出现NullPointerException?错误在哪里?

编辑:在我的move()函数中的第六个循环之后,此行发生错误:snakeButtonPos[i] = snakeButton[i].getLocation();

确切的例外:

Exception in thread "Thread-2" java.lang.NullPointerException
    at Snake.move(Snake.java:92)
    at Snake.run(Snake.java:217)
    at java.lang.Thread.run(Unknown Source)

EDIT2 :我在线程中调用move和Grow

@Override
public void run() {
    while (true)
    {
        CheckPosition(); //CheckPosition includes Grow() if the snake intersects with an object
        move();

        try 
        {
            Thread.sleep(100);
        } 
        catch (InterruptedException ie) 
        {
            System.out.println(ie);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Grow()中,尝试将int newIndex = sizeSnake + 1;更改为int newIndex = sizeSnake;。由于您的for循环一直持续到i < sizeSnake,这意味着最后JButton实际上是snakeButton[sizeSnake - 1]的那个,而不是snakeButton[sizeSnake]的那个。

答案 1 :(得分:2)

由于缺少一些代码,在黑暗中拍摄:

private void Grow()
{
    JButton tempButton = new JButton(); 
    int newIndex = sizeSnake + 1;

    // Add the new Button to the Button Array
    snakeButton[newIndex] = new JButton();
    snakeButton[newIndex].setEnabled(false);

    ...
}

sizeSnake为5,然后newIndex设置为6,因此您的snakeButton数组后面会显示如下:

[button, button, button, button, button, null, button, null, ...]

因为索引6是第七个元素。 <{1}}是不必要的。

newIndex方法应为

move()