Here is the segment of my code The add method functions as an arrayList add method to add a value to a specified index. The size variable increases when declaring this method in the runner, but displaying the new list results in a NullPointException.
public void add(int index, E value) {
ListNode<E> newNode = new ListNode <E>(value);
ListNode <E> tempNode = root;
if(index==0 && root==null) {
root.setPrevious(newNode);
root = root.previous;
}
if(index == size) {
end.setNext(newNode);
end = end.next;
}
if(index>0 && index<size) {
for (int i = 0; i < index-1 ; i++) {
tempNode = tempNode.next;
}
tempNode.next = newNode;
}
size++;
}
public String toString() {
String output = "";
ListNode<E> tempNode = root;
for(int i=0 ; i<size ; i++) {
output+=tempNode.getValue();//figure out why tempNode can be null
if(i!=size-1)
output+=", ";
tempNode = tempNode.next;
}
return output;
}