3f9f2d31e0d42bc91d3135953eb41d2e

why this.getOwned().add(item); returns a java.lang.NullPointerException?

// 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 !

D41d8cd98f00b204e9800998ecf8427e

bob

May 25, 2009, May 25, 2009 20:50, permalink

1 rating. Login to rate!

"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.

3f9f2d31e0d42bc91d3135953eb41d2e

okram87.myopenid.com

May 25, 2009, May 25, 2009 22:02, permalink

No rating. Login to rate!

thank you!

Your refactoring





Format Copy from initial code

or Cancel