I have a function in my Doubly linked list class to insert an element is given index. So I made some codes that did not work.The error they said is "Cannot read field "prev" because "current" is null" .
Here is my code:
public void add(int key, E data){
if(key<0 || key>size){
return;
}
else{
Node current = head;
if (key==0){
Node newLink = new Node(data);
if (size==0)
tail = newLink;
else
head.prev = newLink;
newLink.next = head;
head = newLink;
}
else{
for(int i=0;i<key;i++){
current= current.next;
}
Node newNode= new Node(data);
newNode.next=current;
newNode.prev=current.prev;
size++;
}
}
}
Please help me how can I get the way to overcome this problem.