Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2009, 04:35 PM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
freeBatjko is on a distinguished road
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):
Code:
public class person {

public String name;
public person father;
public ArrayList<person> children;

}

Now I wanted to create relationships:
Code:
person p1 = new person();
p1.setName("Lucy");  

person p2 = new person();
p2.setName("Fred");

p1.father= p2;
p2.children.add(p1);
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:
Code:
p1.setFather(p2);
doesn't work either. same exception.


Can anyone enlighten me, please?

Last edited by freeBatjko; 11-02-2009 at 04:40 PM. Reason: added setter alternative
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-02-2009, 04:44 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 583
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
Could u paste the complete code with code tag?
__________________
Ramya
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-02-2009, 07:59 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,896
Rep Power: 2
r035198x is on a distinguished road
Default
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
Code:
ArrayList<person> children = new ArrayList<person>();
to initialize the list of children before you try to manipulate it.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-02-2009, 09:56 PM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
freeBatjko is on a distinguished road
Default
Originally Posted by r035198x View Post
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
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-03-2009, 03:48 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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:
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:
Code:
public class Person {
    public String name;
    public person father;
    public ArrayList<Person> children = new ArrayList<Person>();
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-03-2009, 10:18 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
freeBatjko is on a distinguished road
Default
Great. Exactly what I needed.
Many thanks, guys. Much appreciated!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
nullpointerexception, recursive

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute() throws an NPE mjz Database 0 08-06-2009 03:25 AM
[SOLVED] NullPointerException even when class is instantiated as expected DCC1 New To Java 17 07-12-2009 07:42 PM
another NullPointerException in LinkedList class muffstuff New To Java 7 04-10-2009 11:51 PM
throws jdgallag New To Java 14 02-11-2009 02:07 AM
throws Exception javaplus New To Java 1 11-06-2007 08:32 PM


All times are GMT +2. The time now is 09:31 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org