Results 1 to 6 of 6
  1. #1
    kudwn is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default 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:

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    As the first line of your constructor print out 'c':

    Java Code:
    System.out.println(c);
    I suspect that c is null; your 'this' reference is fine in a constructor.

    kind regards,

    Jos

  3. #3
    kudwn is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Actually my constructor is slightly more complex than this. I just simplified it a bit for ease of reading. It contains a line to ensure c is not null:

    Java Code:
    public Item(String description, Container c) {
        	if (c != null && c.find(description) == null) {
        		this.container = c;
        		this.description = description;
        		container.add(this);
        	}
    }
    Have any other ideas?

    Thanks!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kudwn View Post
    Actually my constructor is slightly more complex than this. I just simplified it a bit for ease of reading. It contains a line to ensure c is not null:

    Java Code:
    public Item(String description, Container c) {
        	if (c != null && c.find(description) == null) {
        		this.container = c;
        		this.description = description;
        		container.add(this);
        	}
    }
    Have any other ideas?

    Thanks!
    Which line throws the NullPointerException? Even better: show us the entire stack trace.

    kind regards,

    Jos

  5. #5
    kudwn is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    The null pointer exception occurs at line 22, which is the following:

    Java Code:
    container.add(this);
    My original thought was that 'this' is being used as a parameter for a method of an external class before it has even been fully constructed. Would you know of a way to fully construct the Item first, and then add it to the Container?

    I really appreciate your help.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kudwn View Post
    The null pointer exception occurs at line 22, which is the following:

    Java Code:
    container.add(this);
    My original thought was that 'this' is being used as a parameter for a method of an external class before it has even been fully constructed. Would you know of a way to fully construct the Item first, and then add it to the Container?

    I really appreciate your help.
    Object 'this' already has been completely constructed; it may not be completely initialized yet but it already exists and can't be null. Check your member variable 'container'; it is probably the one that is null.

    kind regards,

    Jos

Similar Threads

  1. Using 'this' keyword in constructor
    By kudwn in forum New To Java
    Replies: 2
    Last Post: 10-11-2010, 12:12 PM
  2. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 09:20 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •