Results 1 to 6 of 6
  1. #1
    freeBatjko is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default Recursive Class throws NullPointerException

    Hi all,

    I literally just started with Java and I am experimenting with OOP, but this puzzles me:

    I have something that resembles the following (plus getters, setters etc):
    Java Code:
    public class person {
    
    public String name;
    public person father;
    public ArrayList<person> children;
    
    }

    Now I wanted to create relationships:
    Java Code:
    person p1 = new person();
    p1.setName("Lucy");  
    
    person p2 = new person();
    p2.setName("Fred");
    
    [B]p1.father= p2;
    p2.children.add(p1);[/B]
    However, here is where I get the NullPointerExceptions.
    It doesn't seem to work this way, and I just assume I am not yet in the right mind set for OOP to understand what's wrong here.

    btw, using a setter for the first bold line:
    Java Code:
    p1.setFather(p2);
    doesn't work either. same exception.


    Can anyone enlighten me, please?
    Last edited by freeBatjko; 11-02-2009 at 03:40 PM. Reason: added setter alternative

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Could u paste the complete code with code tag?
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Java Code:
    public ArrayList<person> children;
    does not create a list of person. It only creates a reference that is automatically initialized to null. Trying to add anything to it will throw the N.P.E
    You need to do
    Java Code:
    ArrayList<person> children = new ArrayList<person>();
    to initialize the list of children before you try to manipulate it.

  4. #4
    freeBatjko is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Java Code:
    public ArrayList<person> children;
    does not create a list of person. It only creates a reference that is automatically initialized to null. Trying to add anything to it will throw the N.P.E
    You need to do
    Java Code:
    ArrayList<person> children = new ArrayList<person>();
    to initialize the list of children before you try to manipulate it.
    ah of course! makes sense.

    can i use this as a field in my class though?

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    can i use this as a field in my class though?
    Yes, you can declare the variable and later initialize it in a constructor:
    Java Code:
    public class Person {
        public String name;
        public person father;
        public ArrayList<Person> children;
    
        public Person() {
            children = new ArrayList<Person>();
        }
    }
    or you can declare and initialize it in one step:
    Java Code:
    public class Person {
        public String name;
        public person father;
        public ArrayList<Person> children = new ArrayList<Person>();

  6. #6
    freeBatjko is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Great. Exactly what I needed.
    Many thanks, guys. Much appreciated!

Similar Threads

  1. Execute() throws an NPE
    By mjz in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 02:25 AM
  2. Replies: 17
    Last Post: 07-12-2009, 06:42 PM
  3. another NullPointerException in LinkedList class
    By muffstuff in forum New To Java
    Replies: 7
    Last Post: 04-10-2009, 10:51 PM
  4. throws
    By jdgallag in forum New To Java
    Replies: 14
    Last Post: 02-11-2009, 01:07 AM
  5. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Tags for this Thread

Posting Permissions

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