Results 1 to 9 of 9
Thread: LinkedList Iterator
- 11-17-2011, 03:38 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
LinkedList Iterator
For an assignment, I had to create my own LinkedList class -- called BasicLinkedList -- that has many methods similar to LinkedList. I have to create a BasicListIterator that can iterate through the list, which contains the next(), previous(), hasNext(), and hasPrevious() methods. I'm having trouble with this, and keep getting NullPointerExceptions whenever I try to test it. Please let me know what I'm doing wrong!!
Java Code:private class Node { public E data; public Node next; public Node prev; } private Node head; private class MyListIterator implements BasicListIterator<E> { private Node position = head; public E next() { if(!(hasNext())) { throw new NoSuchElementException(); } else { E data = position.data; position = position.next; return data; } } public E previous() { if(!(hasPrevious())) { throw new NoSuchElementException(); } else { System.out.println(position); E data = position.prev.data; position = position.prev; return data; } } public boolean hasNext() { if(position == null) { return false; } else { return true; } } public boolean hasPrevious() { if(position == head) { return false; } else { return true; } } public void remove() { throw new UnsupportedOperationException(); } }
- 11-17-2011, 03:41 AM #2
Re: LinkedList Iterator
We don't read minds. Post your errors and indicate on which lines they occur.
- 11-17-2011, 03:44 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: LinkedList Iterator
Sorry. Here are my tests. When I call iter.previous(), I get this error:
Exception in thread "main" java.lang.NullPointerException
at BasicLinkedList$MyListIterator.previous(BasicLinke dList.java:38)
Java Code:BasicLinkedList<Integer> list = new BasicLinkedList<Integer>(); int[] ints = new int[] {99, 88, 77, 66, 55}; list.add(ints[0]); BasicListIterator<Integer> iter = list.basicListIterator(); iter.next() == ints[0]; iter.previous() == ints[0];Last edited by cpguy; 11-17-2011 at 03:47 AM.
- 11-17-2011, 03:47 AM #4
Re: LinkedList Iterator
Is that line 38?Java Code:E data = position.prev.data;
When position is head then prev is null. So prev.data will throw a NPE.
- 11-17-2011, 03:49 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: LinkedList Iterator
I changed it to just "position.data" but am still getting the same error...
- 11-17-2011, 03:51 AM #6
Re: LinkedList Iterator
Think about it!
When you are at the head does it have a previous?
- 11-17-2011, 03:52 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: LinkedList Iterator
No -- but we aren't at head. After calling iter.next(), we're at the next node; so prev would be the node we just went over.
- 11-17-2011, 03:57 AM #8
Re: LinkedList Iterator
I see you have a print statement. Change it to print previous.data to see exactly which node is throwing the NPE. Then trace back as to what and why it is null.
- 11-17-2011, 05:47 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Iterator
By Dayanand in forum New To JavaReplies: 2Last Post: 03-10-2011, 12:17 PM -
How to use linkedlist 'add' method without iterator
By plexus0208 in forum New To JavaReplies: 1Last Post: 11-25-2010, 08:27 PM -
Iterator as return and argument or Iterator-String(not visible in the test file)
By Aldarius in forum New To JavaReplies: 0Last Post: 05-18-2010, 12:53 AM -
iterator
By venkatallu in forum Advanced JavaReplies: 3Last Post: 09-23-2008, 01:32 PM -
Iterator
By eva in forum New To JavaReplies: 0Last Post: 01-31-2008, 02:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks