【问题标题】:java.lang.NullPointerException when passing a boolean through an arrayjava.lang.NullPointerException 将布尔值传递给数组
【发布时间】:2014-04-27 05:27:34
【问题描述】:

我正在尝试为我的 Java 课程制作游戏,但我不断获得 NPE。我知道这意味着传递的变量之一是空值,但我不知道在哪里。我检查了所有涉及的变量。我相信初始化数组可能是一个问题,但我仍然没有看到我做错了什么。我检查了堆栈溢出,并且由于各种原因看到了 NPE,但我找不到适用于我的解决方案。

public class Inventory{
public int gold = 0;
private Item[] itemListArray = new Item[30];
private JButton[] itemButtonArray = new JButton[30];
private JButton buttonBack = new JButton("Back");
private static final String HOME = "Home";
public Inventory() {
    for(int i = 1;i < 31; i++)
    {
        itemListArray[i].emptySlot = true; //Here is where the NPE hits
    }
}}

这就是 NPE 要求错误的地方

public class Item {
protected String name = "";
protected int def = 0;
protected int stack = 100;
protected boolean stackable = false;
protected boolean consume = false;
boolean emptySlot = true;
protected ImageIcon icon;
public Item(){

}
public boolean isArmor()
{
    if(def >= 1)
    {
    return true;
    } else {
    return false;
    }
}
public boolean isConsumable()
{
    if(consume = true)
    {
        return true;
    } else {
        return false;
    }
}
public boolean isEmpty()
{
    if(emptySlot = true)
    {
        return true;
    } else {
        return false;
    }
}

这里是Item的声明。

请尽快回答我的问题,我似乎无法弄清楚。

【问题讨论】:

标签: java arrays boolean


【解决方案1】:
Item[] itemListArray = new Item[30];

此代码只是创建了一个包含null 值的数组,您需要初始化数组中的每个单独的值。

for(int i = 1;i < 31; i++)
{
    itemListArray[i].emptySlot = true; //Here is where the NPE hits
}

这个循环会在后面导致ArrayIndexOutOfBoundsException,因为在Java中有效的数组索引从0开始到array.length-1(在你的情况下是0到29),而这段代码将尝试访问itemListArray[ 30 ]

【讨论】:

    【解决方案2】:

    实例化数组是不够的,还必须用对象填充它。否则每个索引默认包含null。

    private Item[] itemListArray = new Item[30];
    for (int i = 0; i < itemListArray.length; i++) {
        itemListArray[i] = new Item();
    }
    

    【讨论】:

      【解决方案3】:

      您使用 private Item[] itemListArray = new Item[30]; 实例化您的数组,这将创建一个 Item 类型的数组,其中包含 30 个空条目。

      当您在构造函数的循环中调用itemListArray[i].emptySlot 时,您正在从一个空对象访问一个变量。

      您必须先在构造函数(或其他地方)的循环中实例化数组中的任何 Item 对象,然后才能访问任何变量或从中调用任何方法。

      您的for 循环也正在跳过第一个元素。 Java 中的第一个元素的索引为 0。

      【讨论】:

        【解决方案4】:

        我猜你可能没有理解java中的初始化。你只是初始化一个数组,但它并没有引用真实的对象。

        这样的代码会有所帮助:

        for(int i = 1;i < 31; i++){   
                Item item = new Item();
                item.emptySlot = true;
                itemListArray[i] = item; //Here is where the NPE hits
        }
        

        尝试在 Item 类中使用构造函数会好得多,希望它能起作用。

        【讨论】:

          【解决方案5】:

          创建一个对象数组会将它们全部默认为空。您需要将一个对象放入数组的每个元素中才能摆脱这个问题。

          for (int i = 0; i < itemListArray.length; i++) {
              itemListArray[i] = new Item();
          }
          
          for (int j = 0; j < itemButtonArray.length; j++) {
              itemButtonArray[j] = new JButton();
          }
          

          【讨论】:

            猜你喜欢
            • 2013-09-04
            • 2015-12-26
            • 2015-08-03
            • 1970-01-01
            • 2022-11-25
            • 2016-10-24
            • 1970-01-01
            • 2018-01-20
            • 2015-09-08
            相关资源
            最近更新 更多