public void remove(E del) { //this deletes head node
if (size == 0) {
}
else {
Node<E> temp = head; //start at head
head = head.next; //head points to head.next
temp = head.next; //move to the next node after head
temp.prev = null; //set prev equal to null
head.next = null; //setting head's next equal to null to make empty
}
}
public String toString() {
Node<E> temp = head; //temp node starting at head to walk the list
if (temp == null) return ""; //if there is nothing to start at return nothing
String output = "[ ";
for(int i = 0; i < size; i++) { //cycle through while i < size
if(i == 0) { //if is index 0, then grab the data of head
output += temp.data;
}
else { //else if i is more than 0, then grab the data of head and display with output
output += ", \n" + temp.data;
}
temp = temp.next; // sets the temporary head equal to temp.next;
}
output += " ]";
return output;
}
I am trying to create a remove() method that will remove the head node of the double linked list. I have tried many different ways to make this method work but every time I run the program, I am always method with "NullPointerException: Cannot read field "data" because "temp" is null" and I am not sure why I keep getting this error. When I read the error it always points back to line 191 which happens to be in the toString() method.
To remove the head node I figured that I would have to create a temp node that starts at the head node. After starting at the head node, I would use head = head.next to point to the next of head and then I would set that to head.next = null so it would severe the connection between the head and the node after it.