Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

beginner question about null pointer exceptions

Thread view: 
aa - 24 Jul 2006 08:52 GMT
Hi, I'm a beginner programmer and I keep getting stuck on this one
problem:
I try to make a new node:
   TreeNode newTree = new TreeNode(someitem(),somechild());
but, I keep getting null pointer exceptions on this line:
   Child.setItem(kids.head.listItem);
does that mean that either Child or  kids.head.listItem doesnt exist?

public class TreeNode {
 private Object item;
 private TreeNode Child;
 private TreeNode Sibling;
 public TreeNode(Object item, SList kids) {
   this.item = item;
    Child.setItem(kids.head.listItem);          //   this one
 }

public class SList {
 public SListNode head;
 public SList() {
   head = null;
 }
 public SList(SListNode head) {
   this.head = head;
 }

public class SListNode {
 public Object listItem;
 public SListNode next;
 public SListNode(Object item) {
   this.listItem = item;
 }
Moiristo - 24 Jul 2006 09:42 GMT
> Hi, I'm a beginner programmer and I keep getting stuck on this one
> problem:
[quoted text clipped - 3 lines]
>     Child.setItem(kids.head.listItem);
> does that mean that either Child or  kids.head.listItem doesnt exist?

It could mean that Child, kids, or head does not exist.
Moiristo - 24 Jul 2006 09:43 GMT
>> Hi, I'm a beginner programmer and I keep getting stuck on this one
>> problem:
[quoted text clipped - 5 lines]
>
> It could mean that Child, kids, or head does not exist.

Looking at your code, you've probably forgotten to initialize Child
Papastefanos Serafeim - 24 Jul 2006 09:45 GMT
I don't see a constructor in TreeNode...
Before using Child.setItem(...), you have to instantiate the
Child object by using TreeNode Child = new TreeNode();

Also, another hint: Don't capitalize object names. It'd be
better to use private TreeNode child, private treeNode
sibling.

Signature

Papastefanos Serafeim

> Hi, I'm a beginner programmer and I keep getting stuck on this one
> problem:
[quoted text clipped - 28 lines]
>    this.listItem = item;
>  }
Patricia Shanahan - 24 Jul 2006 10:22 GMT
> Hi, I'm a beginner programmer and I keep getting stuck on this one
> problem:
[quoted text clipped - 3 lines]
>     Child.setItem(kids.head.listItem);
> does that mean that either Child or  kids.head.listItem doesnt exist?

It means that at least one of the references Child, kids, or kids.head
is null. Null is a special value a reference variable has when it does
not point to any object.

> public class TreeNode {
>   private Object item;
[quoted text clipped - 4 lines]
>      Child.setItem(kids.head.listItem);          //   this one
>   }

Child is definitely null. A reference field is null unless it has a
non-null initializer or you have assigned something to it.

However, you will need to be careful about how you initialize it. You
cannot have the only TreeNode constructor unconditionally call itself to
create a new TreeNode for Child or Sibling. If you did that, the
constructor would loop until it ran out of stack space.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.