I'm implementing a doubly linked list with a Previous() function that raises a NullPointerException, I would appreciate help if someone knows how to fix this. The error is in line 37 of the parent class (DoublyLinkedList).
I'm a beginner taking (a beating from) a course on data structures and java. A friend helped me by saying the following "It says that your null pointer exception is at your .Previous() function [line 18]. I looked at your Previous() function within your DoublyLinkedList class and it seems that you don't check if current == null before testing if current != head. YOu must have a null case so that you don't get a nullpointer like in your case. If your current == null, you should set it to the tail, otherwise keep the rest of our code." I tried fixing that but still have the same problem
class Node
{
// Public references to previous and next nodes
public Node next;
public Node previous;
// Private data field
Object data;
public Node(Object data)
{
this.data = data;
}
public Object GetData()
{
return data;
}
}
class DoublyLinkedList
{
Node head;
Node tail;
Node current;
Node next;
Node previous;
// Go to the first element in the list
public void Head()
{
current = head;
previous = null;
}
//Go to the last element in the list
public void Tail()
{
current = tail;
next = null;
}
// Go to the next element if the current element is not past-the-end
public void Next()
{
if (current != null)
{
previous = current;
current = current.next;
}
}
public void Previous()
{
if (current == null && current != head)
next = null;
Tail();
if (current != head)
{
next = current;
current = current.previous;
}
}
// Return the data associated with the current element
// or null if the current element is past-the end
public Object Get()
{
return current == null ? null : current.GetData();
}
// Insert a new element before the current element
// or at the end if the current element is past-the-end
public void Insert(Object data)
{
// Create new node
Node node = new Node(data);
// Set 'next' reference of new node
node.next = current;
// Set 'next' reference for 'previous' node.
// Treat the special case where the current node is the head.
if (current == head)
head = node;
else
previous.next = node;
// Update current node
current = node;
}
public void Print()
{
// Traverse list
for (Head(); Get() != null; Next())
System.out.println(Get());
}
}
class Test
{
public static void main(String args[])
{
// Create list
DoublyLinkedList doubly_linked_list = new DoublyLinkedList();
// Insert elements at the end
doubly_linked_list.Insert("a");
doubly_linked_list.Next();
doubly_linked_list.Insert("b");
doubly_linked_list.Next();
doubly_linked_list.Insert("c");
doubly_linked_list.Next();
// Set second-to-last as current element. Insert string "d"
doubly_linked_list.Tail();
doubly_linked_list.Previous();
doubly_linked_list.Insert("d");
// Set current element as past-the-end. Insert string "e"
doubly_linked_list.Tail();
doubly_linked_list.Next();
doubly_linked_list.Insert("e");
// Print
doubly_linked_list.Print();
}
}