0

I am creating an array list in a class called Inventory. Here there are methods to add to, delete and get the list. I create a new array list in a new class called combat and set this array list to the one in the Inventory class with a get method.

This is the inventory class part:

private ArrayList<String> inventory;

public Inventory() {
    this.inventory = new ArrayList<>();
}

public void addToInventory(String item) {
    inventory.add(item);
    System.out.println("A " + item + " has been added to your inventory");
    printInventory();
}

public void removeFromInventory(String item) {
    inventory.remove(item);
}

public ArrayList<String> getInventory() {
    return inventory;
}

This is in the Combat Class:

private Inventory inventory = new Inventory();
private ArrayList<String> playerInventory = inventory.getInventory();

public Combat() {

}

@Override
public void setPlayerHit() {
    System.out.println(playerInventory);

When I call the print playerInventory line, the arrayList returns []. Yet in the inventory class it has something in as I add an item to it shown in my launcher class:

playerInventory.addToInventory(item.getItem(9));

I can get this working using public fields but wanted to make use of encapsulation and use getters and setters. Could I possibly get some pointers please?

5
  • 2
    You have not given us all necessary elements to reproduce the problem. Can you make a Minimal, Complete, and Verifiable example? Commented Apr 13, 2017 at 13:11
  • Do you add something to the inventory before printing playerInventory list? Commented Apr 13, 2017 at 13:12
  • 1
    playerInventory is an ArrayList. How is that you call addToInventory() method on it? Commented Apr 13, 2017 at 13:12
  • Could you please provide a sequence of constructor and method calls before and after playerInventory.addToInventory(item.getItem(9)); ? Commented Apr 13, 2017 at 13:30
  • Considering that your question was solved, consider learning about dependency injection (if you don't already know it) and a framework like Google Guice. It will be really useful :-) Commented Apr 13, 2017 at 23:33

2 Answers 2

2

Just guessing here, but it sounds like you are using separate Inventory objects in different parts of the code. They won't share the same internal inventory array list and items added to one Inventory object won't appear in the other. Make sure that the Inventory object you are printing is the same one to which you have added items.

If you want a single global Inventory object, you can use a singleton:

public class Inventory {
    private static Inventory instance = new Inventory();
    private ArrayList<String> inventory;

    public static Inventory getInstance() {
        return instance;
    }

    // private constructor to prevent extra copies of the inventory
    private Inventory() {
        inventory = new ArrayList<>();
    }

    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

exactly my thoughts :), though private Inventory instance should be static
@Yazan - Yes, it should, and now it is :). Thanks for catching that.
0

When I call the print playerInventory line, the arrayList returns []

you are not inserting any string in the Inventory... thus the empty list...

by the way, this is redundant:

private ArrayList<String> playerInventory = inventory.getInventory();

since you are using by composition an inventory object wich has the ArrayList

try again but before doing

public Combat() {
    inventory.addToInventory("f1");
    inventory.addToInventory("f2");
}

now your inventory object has 2 elements

1 Comment

i think it would be better if OP passes instance of Inventory to Combat ? and just assign it to the member Inventory ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.