0

I'm trying to build a remove function to remove a particular organ from the list. I get an error of NullPointerException I would like to know what my mistake is. Why am I getting this error. Thanks.

A method to delete a specific organ. The method returns the deleted organ, organ lab is not on the list, it returns null

import java.util.ListIterator;
import java.util.NoSuchElementException;

public class LinkedListDouble<T> {
    private LDNode<T> head;
    private LDNode<T> tail;
    public int size;


LinkedListDouble(){
    this.head = null;
    size = 0;
}


public void add(T item){ // add item to the list
    if(head == null ){
        head = new LDNode<T>(item,null,null);
    }
    else{
        LDNode<T> n = new LDNode<T>(item, null, null);
        while(n.next != null ){
            n = n.next;
        }
        n.next = new LDNode<T>(item,n,null);
    }size++;
}

public T remove(T item){ // remove item from the list
    T ans=null;
    LDNode<T> n = head;
    int i=0;

    while(n.data != item){
        n = n.next;
        i++;
    }
    if(i == size) return null;
    if(n == head) {
        ans = head.data;
        head.prev.next = head.next;
        head.next.prev = head.prev;
        head = head.next;
    }
    else {
        ans = n.data;
        n.prev.next = n.next;
        n.next.prev = n.prev;
    }
    size--;
    return ans;

    }

public int size(){
    return size;
}

Node:

public class LDNode<T> {
T data;
LDNode<T> next,prev;


LDNode(){
    next = null;
    prev = null;
    data = null;
}

/*LDNode(T data){
    this(data,null,null);
}*/
LDNode(T data , LDNode<T> next , LDNode<T> prev){
    this.data = data;
    this.next = next;
    this.prev = prev;
}
LDNode(LDNode<T> Other){
    this.data = Other.data;
    this.next = Other.next;
    this.prev = Other.prev;
}

T getData(){
    return data;
}

public void setNextNode(LDNode<T> next){
    this.next = next;
}
public LDNode<T> getPrevNode(){
    return prev;
}
public void setPrevNode(LDNode<T> prev){
    this.prev = prev;
}
public void setData(T data){
    this.data = data;
}
public LDNode<T> getNextNode(){
    return next;
}

}

Main:

    public static void main(String[] args) {
    LinkedListDouble<Integer> itay = new LinkedListDouble<>();
    itay.add(1);
    itay.add(2);
    itay.add(3);
    itay.add(4);
    itay.add(5);
    itay.add(6);
    System.out.println(itay.size());
    itay.remove(1);
    System.out.println(itay.size());
}

Error:

Exception in thread "main" java.lang.NullPointerException
at Matala2.LinkedListDouble.remove(LinkedListDouble.java:45)//head.prev.next = head.next;
at Matala2.LinkedListDouble.main(LinkedListDouble.java:78) //itay.remove(1);
Daedric
  • 512
  • 3
  • 22
itay izraelov
  • 47
  • 1
  • 1
  • 5
  • Either `head` is `null` or `head.prev` is. `head` cannot be, or you would have got the exception one line earlier. So `head.prev` is null, so you r attempt to assign to `head.prev.next` gives NPE. – Ole V.V. Dec 30 '16 at 16:25
  • What had you expected that `head.prev` should have referred to? Without studying every line in your code, I would have expected it to be `null`. – Ole V.V. Dec 30 '16 at 16:27
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ole V.V. Dec 30 '16 at 17:30

1 Answers1

1

The first foreseen problem is in the add method of the LinkedListDouble class. See, you're creating head and after that don't link it with any other instance of the LDNode class. So, head always have null as prev, that's why you're getting NPE. Moreover, a code inside the add method never places any different from null as prev value of LDNode object. Consider revising your add method of the LinkedListDouble class.

arcquim
  • 990
  • 1
  • 13
  • 21