Results 1 to 2 of 2
Thread: Null Pointer Exception
- 01-24-2009, 10:53 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
Null Pointer Exception
Hi All,
I am working on a code for linked lists and am encountering a strange null pointer exception error that I don't understand in the following code:
I have test code that loads the linked list and when it tries to 'add' an element to the array the program stops on the bold statement, where I am trying to change the value of the head.item location to something other than null. Why do I get an exception here?Java Code:public class Node { public Comparable item; public Node next; public Node(Comparable i) { item = i; next = null; } } private Node head; private Node iterator; public MySortedLinkedList() { head = null; iterator = null; } public void add(Comparable item) { if (iterator == null) { [B]head.item = item;[/B] } }
Cheers,
ScKaSx
- 01-24-2009, 11:27 AM #2
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
Hi All,
For anyone that is curious about this, I found the solution. I thought I initialized the Node head when I wrote:
however, this did not create a new node. Therefore the correction for the bold statement would be:Java Code:head = null;
How come coding can't always be intuitive?Java Code:head = new Node(item);
Cheers,
ScKaSx
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 06:33 PM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks