Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > beginner question about null pointer exceptions

Reply
Thread Tools

beginner question about null pointer exceptions

 
 
aa
Guest
Posts: n/a
 
      07-24-2006
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;
}

 
Reply With Quote
 
 
 
 
Moiristo
Guest
Posts: n/a
 
      07-24-2006
aa wrote:
> 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?


It could mean that Child, kids, or head does not exist.
 
Reply With Quote
 
 
 
 
Moiristo
Guest
Posts: n/a
 
      07-24-2006
Moiristo wrote:
> aa wrote:
>> 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?

>
> It could mean that Child, kids, or head does not exist.


Looking at your code, you've probably forgotten to initialize Child
 
Reply With Quote
 
Papastefanos Serafeim
Guest
Posts: n/a
 
      07-24-2006
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.

--
Papastefanos Serafeim
? "aa" <> ?????? ??? ??????
news: oups.com...
> 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;
> }
>



 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-24-2006
aa wrote:
> 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?


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;
> private TreeNode Child;
> private TreeNode Sibling;
> public TreeNode(Object item, SList kids) {
> this.item = item;
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
Null pointer exceptions Alan Java 4 12-28-2007 01:55 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
null pointer exceptions Tennessee James Leeuwenburg Python 1 07-18-2003 05:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57