Using 'this' keyword in constructor
So I've ran into a small problem here that I can't seem to get around. I'm writing a class Item and when the constructor is invoked, I need the Item to be added to an instance of another class Container. Here is the code for the constructor:
Code:
public Item(Container c) {
c.add(this);
}
The problem is that I get a null pointer exception when calling the method add in for the Container. I think the problem lies in my using the 'this' keyword in the constructor, before the object has been fully constructed. Any ideas for getting around this?
Thanks!