0

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.

ndsmith
  • 37
  • 8
  • Do you think you could edit the question to show your error message, and also to explain where and how you're initializing your array, and how you're calling it, and what you've tried? – Superhuman Oct 17 '19 at 23:44
  • also is it the array that is null or an object in the array that is null – Superhuman Oct 17 '19 at 23:45
  • Edited to show error message. My code shows initialization and how it is being called – ndsmith Oct 17 '19 at 23:58
  • The `static` keyword markes `treasures` as shared among all instances of the `item` class. This essentially makes `treasures` a global variable. When new instances of `item` are created the `treasures` passed into the constructor overwrite the shared variable. It's likely `new item("Necklace of Harming"... null)` is executed last which squashes the shared ArrayList. If every item should have the same `treasures` then I'd recommend dropping it from the constructor. Otherwise, drop the `static` keyword. – theMike Oct 18 '19 at 00:13
  • @theMike Dropping it from the constructor worked perfectly. That is not something that I ever would have thought of. Thank you so much! – ndsmith Oct 18 '19 at 00:25

1 Answers1

0

Try initializing your arraylist like the following: static List<item> treasures = new ArrayList<>();

this is how I initialize my ArrayLists, and it works like a charm.

Superhuman
  • 75
  • 9