java - Doubly Linked List Null Pointer Exception -
i'm trying work on remove function doubly linked list, keep getting null pointer exception on current.prev.next = current.next;
part. don't think understand null pointer exception is, because have no idea fix this. log function 1 wrote write output file, , retval[1] element i'm searching delete.
node current = head; while(current != null) { if((current.data).compareto(retval[1]) == 0) { if(current.prev == null) head = current.next; if(current.next == null) tail = current.prev; current.prev.next = current.next; current.next.prev = current.prev; current = null; valid++; log(line + "\n" + "sucsessfully removed \n"); } else { log(line + "\n" + invalidtransaction + " - element not exist \n"); } current = current.next; }
i'm sure it's stupid, don't know is. appreciated.
just replace
current.prev.next = current.next; current.next.prev = current.prev;
with
if(null != current.prev) current.prev.next = current.next; if(null != current.next) current.next.prev = current.prev;
and need break loop once element found.
Comments
Post a Comment