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!!
<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;
}
}
I'm having trouble with this, and keep getting NullPointerExceptions whenever I try to test it.
This code won't even compile. Please make sure you have posted the code you are running and also post the exceptions you are getting and explain when they occur..
Bookmarks