Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
因此,我创建了一个小蛇游戏。
在我的构造函数中,我调用“ CreateSnake”函数,该函数使用JButton创建一个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时,在move方法中都会得到一个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:我在“线程”中调用“移动并增长”

@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);
        }
    }
}

最佳答案

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

09-10 08:53
查看更多