My ArrayList has been initialized, as you can see from the below code, but I am still getting a null object reference error.
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;
}
It is getting the int from a combat class using this method:
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);
}
}
This is the error that I am getting.
2019-10-17 18:20:21.903 7907-7907/com.example.treasurehunt E/AndroidRuntime: FATAL EXCEPTION: 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)
There is a lot more to the program that I" am making, but none of it pertains to this error. Any help is appreciated. Thanks in advance.