0
package LinkList2;
//import java.util.*;


public class Duplicates {

    public static void removeDuplicates(LinkedListNode head)
    {
        LinkedListNode current = head;

        while(current!= null && current.next!= null)
        {
            LinkedListNode curr = current;

            while(curr!=null)
            {
                if(curr.next.data==current.data) //Getting error at this line
                    curr.next = curr.next.next;

                else
                    curr = curr.next;
            }

            current = current.next;
        }
    }


    public static void main(String args[])
    {
        LinkedListNode first = new LinkedListNode(0,null,null);
        LinkedListNode head = first;
        LinkedListNode second = first;


        for(int i=1; i< 8; i++)
        {
            second = new LinkedListNode(i%2, null, null);
            first.setNext(second);
            second.setPrevious(first);
        }

        System.out.println(head.printForward());
        removeDuplicates(head);// Getting error at this line
    }

}

Getting null pointer exception in the above code. When I try to run the above code, it gives null pointer exception. Please help me with my mistake.

Below is the implementation of LinkList where all the methods are defined

 class LinkedListNode {
    public LinkedListNode next;
    public LinkedListNode prev;
    public LinkedListNode last;
    public int data;
    public LinkedListNode(int d, LinkedListNode n, LinkedListNode p) {
        data = d;
        setNext(n);
        setPrevious(p);
    }

    public void setNext(LinkedListNode n) {
        next = n;
        if (this == last) {
            last = n;
        }
        if (n != null && n.prev != this) {
            n.setPrevious(this);
        }
    }

    public void setPrevious(LinkedListNode p) {
        prev = p;
        if (p != null && p.next != this) {
            p.setNext(this);
        }
    }   

    public String printForward() {
        if (next != null) {
            return data + "->" + next.printForward();
        } else {
            return ((Integer) data).toString();
        }
    }

    public LinkedListNode clone() {
        LinkedListNode next2 = null;
        if (next != null) {
            next2 = next.clone();
        }
        LinkedListNode head2 = new LinkedListNode(data, next2, null);
        return head2;
    }
}
Rafael
  • 7,002
  • 5
  • 43
  • 52
Yogesh D
  • 1,558
  • 14
  • 29
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – jhamon Jan 06 '16 at 08:27

2 Answers2

1

The problem is that here:

while(curr != null)
{
   if(curr.next.data==current.data) //Getting error at this line
      curr.next = curr.next.next;
   else
      curr = curr.next;
}

You are accessing the curr.next.data where you are not checking if that node is null or not. This through your NullPointerException.

To fix your problem is to check on the while loop, if the .next is also not null.

while(curr != null && curr.next != null)
{
   if(curr.next.data==current.data) //Getting error at this line
      curr.next = curr.next.next;
   else
      curr = curr.next;
}

In other words, you are not checking if your next node is actually the end of the linked list (i.e null). If you need in your program logic to handle this separately, then you should remove the check from the while loop, and implement this check differently.

Rafael
  • 7,002
  • 5
  • 43
  • 52
1

You are getting an exception just because of the following condition:

while(curr != null)

Replace it with while(curr != null && curr.next != null) this way you can check if you have the next element.

Hope this helps.

KRP
  • 38
  • 5