java - 通过访问 JButton 数组中的对象出现 NullPointerException

标签 java swing nullpointerexception

<分区>

所以我创建了一个小贪吃蛇游戏。 在我的构造函数中,我调用了“CreateSnake”函数,它使用 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 数组时,我的移动方法中就会出现空指针异常。

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 数组中,而我在我的 Grow() 函数中。但是在下一次调用 move() 时,不再有索引为 6 的 JButton。

为什么会出现 NullPointerException?哪里错了?

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

确切的异常(exception):

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] 处的那个.

关于java - 通过访问 JButton 数组中的对象出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27367989/

相关文章:

Javaagent 报告 "redefineClasses is not supported in this environment"

java - java中的虚方法调用-多态性

java - GridBagLayout:如何使用首选宽度水平垂直填充

java - 从 jpanel 添加/删除 jlabel

java - Android中为什么无法从SqlLiteDatabase读取数据

java - 如何通过catalina.out查找tomcat自动关机原因

java - 显示 JWindow 一段时间

java - 尝试使用字段数组作为参数时出现 NullPointerException

java - JOptionPane 中的选项值

java - NullPointerException InAppBilling.IabHelper.launchPurchaseFlow(IabHelper.java :356)