Results 1 to 3 of 3
- 10-11-2010, 08:37 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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:
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?Java Code:public Item(Container c) { c.add(this); }
Thanks!Last edited by kudwn; 10-11-2010 at 08:40 AM. Reason: formatting
- 10-11-2010, 09:10 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Crosspost
Regards.
-
Similar Threads
-
this keyword
By coltragon in forum New To JavaReplies: 10Last Post: 03-01-2010, 09:20 AM -
How to use Switch keyword
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:07 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Use of this keyword
By Java Tip in forum Java TipReplies: 0Last Post: 11-18-2007, 07:32 PM


LinkBack URL
About LinkBacks


Bookmarks