// FIELD
private ArrayList<Item> owned = new ArrayList<Item>(); // in the Constructor I inizialize this field to null
// GETTER
public ArrayList<Item> getOwned() { return owned; }
// METHOD*
public void addOwned(Item item) {
this.getOwned().add(item); //why this line returns a java.lang.NullPointerException?
}
Refactorings
No refactoring yet !
bob
May 25, 2009, May 25, 2009 20:50, permalink
"in the Constructor I inizialize this field to null" <- well that's the problem. The constructor runs after the field initializer, so you initialize it to a new object and then set it to null.
why this.getOwned().add(item); returns a java.lang.NullPointerException?