gpt4 book ai didi

java - 获取空对象引用但 ArrayList 已初始化

转载 作者:行者123 更新时间:2023-12-01 22:55:24 25 4
gpt4 key购买 nike

我的 ArrayList 已初始化,如下面的代码所示,但我仍然收到空对象引用错误。

String name;
int health;
int stamina;
int magic;
int strength;
Integer picture;
Integer croppedPicture;
static ArrayList<item> treasures = new ArrayList<>();

public item(String name, int health, int stamina, int magic, int strength, Integer picture, Integer croppedPicture, ArrayList<item> treasures){
this.name = name;
this.health = health;
this.stamina = stamina;
this.strength = strength;
this.magic = magic;
this.picture = picture;
this.treasures = treasures;
this.croppedPicture = croppedPicture;
}

static item[] adventurers = {new item("Forhum", 100, 5, 5, 20,R.drawable.forhum,R.drawable.forhum_cropped, treasures),
new item("Triton", 100, 5, 5, 20,R.drawable.triton, R.drawable.triton_cropped, treasures),
new item("Meravan", 100, 5, 5, 20,R.drawable.meravan, R.drawable.meravan_cropped, treasures)};

static item[] treasureList = {new item("Ring of Health", 10, 0, 0, 0, R.drawable.ring_of_health, null, null),
new item("Ring of Stamina", 0, 5, 0,0, R.drawable.stam_green, null, null),
new item("Ring of Strength", 0,0,0,5, R.drawable.ring_of_strength, null, null),
new item("Cup of Magic", 0,0,4,0, R.drawable.cup_of_magic, null, null),
new item("Invisible Necklace", 0,0,0,0, null, null, null),
new item("Necklace of Harming", -10, 0,0,0, R.drawable.necklace, null, null)};

public String getName(){
return name;
}

public int getHealth(){
return health;
}

public int getMagic() {
return magic;
}

public int getStamina() {
return stamina;
}

public int getStrength() {
return strength;
}

public ArrayList<item> getTreasures() {
return treasures;
}

public void setHealth(int health) {
this.health = health;
}

public void setMagic(int magic) {
this.magic = magic;
}

public int getPicture() {
return picture;
}

public void setTreasures(int treasure) {
treasures.add(treasureList[treasure]);
}

public Integer getCroppedPicture() {
return croppedPicture;
}

它使用此方法从战斗类获取 int:

    public void turn(){
setText();
if(monsterHealth > 0) {
if (monsterMagic >= 5) {
monsterUseMagic();
setText();
} else {
monsterAttack();
setText();
}
if(item.adventurers[personNumber].getHealth() > 0) {
item.adventurers[personNumber].setMagic(item.adventurers[personNumber].getMagic() + 1);
monsterMagic++;
setText();
}
else{
died();
}
}
else {
Intent won = new Intent(this, itemSelect.class);
int treasure = rand.nextInt(6);
int view = 2;
won.putExtra("view", view);
won.putExtra("thing", treasure);;
item.adventurers[personNumber].setTreasures(treasure);
startActivity(won);
}
}

这是我收到的错误。

2019-10-17 18:20:21.903 7907-7907/com.example.treasurehunt E/AndroidRuntime:致命异常:main

Process: com.example.treasurehunt, PID: 7907

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference

at com.example.treasurehunt.item.setTreasures(item.java:75)

at com.example.treasurehunt.CombatScreen.turn(CombatScreen.java:118)

我正在制作的程序还有很多内容,但没有一个与此错误有关。感谢任何帮助。提前致谢。

最佳答案

尝试像下面这样初始化你的数组列表: static List<item> treasures = new ArrayList<>();

这就是我初始化 ArrayList 的方式,它的工作原理就像一个魅力。

关于java - 获取空对象引用但 ArrayList 已初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58442029/

25 4 0