我正在尝试为我的Java类制作游戏,但我一直在获取NPE。我知道这意味着其中一个变量被传递为null,但我不知道在哪里。我已经检查了所有涉及的变量。我相信这可能是一个初始化数组的问题,但我仍然没有看到我做错了什么。我已经检查过堆栈溢出,并且由于各种原因已经看到NPE,但是我找不到适用于我的解决方案。当通过数组传递布尔值时发生java.lang.NullPointerException
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;
}
}
这里是项目的申报。
请回答我的问题,我似乎无法弄清楚。
NPE的确有一个原因*。你试图使用的是'null' –
可能重复的[Java数组,NullPointerException?](http://stackoverflow.com/questions/15170192/java-array-nullpointerexception)或任何其他100次这个已被问到。 –