-3
public void deleteSmaller(int data){
    System.out.printf("Deleting data smaller than %d",data);
    System.out.println("");
    dNode current = head;
    dNode lastCurrent;
    lastCurrent = head.Previous;
    dNode nextCurrent = current.Next;
    while(current!= null){
        
        if(data>=current.data){
            if(current.Previous == null){
               nextCurrent.setPrevious(null);
            }else{
            lastCurrent.setNext(nextCurrent);
            nextCurrent.setPrevious(lastCurrent);
            
           }
        }
       current=current.getNext();
       lastCurrent=current.Previous;
       nextCurrent=current.Next;
    }
}

I want to delete data of all the nodes smaller than the given data but it keeps throwing null pointer exception.

The question got closed but i tried it myself updated it and now it is working fine .The code below works just fine.

public void deleteSmaller(int data){
    System.out.printf("Deleting data smaller than %d",data);
    System.out.println("");
    dNode current = head;
    dNode lastCurrent=null,nextCurrent=current.Next ;
    while(current.Next!= null){
        
        if(data>=current.data){
            if(current.Previous == null){
               nextCurrent.setPrevious(null);
            }else{
            lastCurrent.setNext(nextCurrent);
            nextCurrent.setPrevious(lastCurrent);
            
            }
        }
       current=current.getNext();
       lastCurrent=current.Previous;
       nextCurrent=current.Next;
    }
}

2 Answers2

0
public void deleteSmaller(int data){
    dNode current = head;
    dNode lastCurrent;
    lastCurrent = head.Previous; // (1)
    dNode nextCurrent = current.Next;
    while(current!= null){
        if(data>=current.data){
            if(current.Previous == null){
               nextCurrent.setPrevious(null); // (2)
            }else{
            lastCurrent.setNext(nextCurrent);
            nextCurrent.setPrevious(lastCurrent); // (3)
           }
        }
       current=current.getNext();
       lastCurrent=current.Previous;
       nextCurrent=current.Next;
    }
}

(1) If your list was empty, i.e. head is null, calling previous will result in an NPE.

(2) If your list has only 1 node, i.e. nextCurrent of a head node is null, calling setPrevious will result in an NPE.

(3) If your current node is the last one, nextCurrent will be null, same will occur as above.

You should do null checking before assigning or calling any of dNode's methods.

DarthQuack
  • 1,254
  • 3
  • 12
  • 22
0

You are checking if the previous node is null, which is fine since the current node can be a head, but what about when the current is a tail(i.e. last node in the linked list)? you need to check also if the next node is null, ALSO if current is the only node in the list and need to be removed (i.e. next AND previous are null)

 while(current!= null){
        
        if(data >= current.data){
            if (current.Next == null && current.Previous == null)
            {
                current = null;
                break;
            }

            if(current.Previous == null){
               nextCurrent.setPrevious(null);
            }else if(current.Next == null){
               previousCurrent.setNext(null);
            }else{
            lastCurrent.setNext(nextCurrent);
            nextCurrent.setPrevious(lastCurrent);
            
           }
        }
       current=current.getNext();
       lastCurrent=current.Previous;
       nextCurrent=current.Next;
    }
Bahij.Mik
  • 1,358
  • 2
  • 9
  • 22