0

emphasized text i need to pass header of the LINKED LIST and data to be inserted in that linked list.assuming the list is in sorted order i need to check data from each node and insert new node to give newly sorted list.

im getting null pointer exception ,, i need to know what im doing wrong

/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  class Node {
     int data;
     Node next;
     Node prev;
  }
*/

Node SortedInsert(Node head,int data) {
    Node root= head;
    if(head==null){
        root.data=data;
        root.next=null;
        root.prev=null;

    }else if(head.data>data){
            Node newnode = new Node();
           newnode.data=data;
           newnode.next=head;
           newnode.prev=null;
            head.prev=newnode;
            root=newnode;
        }
    int k=0;
    while(head!=null && k==0){

        if(head.data<data && head.next.data>data && head.next!=null){
           Node temp=head.next;
           Node newnode = new Node();
           newnode.data=data;
           newnode.next=temp;
           newnode.prev=head;
           head.next=newnode;
           temp.prev=newnode;k++; break;
       }
        else if(head.data<data && head.next==null){
           //Node temp=head.next;
           Node newnode = new Node();
           newnode.data=data;
           newnode.next=null;
           newnode.prev=head;
           head.next=newnode;k++;break;
           //temp.prev=newnode;
       }else 
       {head=head.next;}

    }
  return root;
}

im getting null pointer exception at second if statement inside while loop.

  • Consider the very first line in your `if`s: You say `head==null` and `root=head`. Then `root.data` is `null.data`. – bleistift2 Apr 15 '17 at 08:36
  • 1
    In the future you should always detail which lines throw the error(s) to help us understand your code. And _please_ re-read your question before posting. – bleistift2 Apr 15 '17 at 08:49
  • @bleistift2 sorry about that.my error message can be read only if the code lines are numbered . i have changed my code as u suggested but still facing same issue. –  Apr 15 '17 at 09:31
  • http://stackoverflow.com/questions/40939732/inserting-a-number-into-a-linked-list-and-keeping-it-sorted-while-doing-so-in-ja/40940375#40940375 – denvercoder9 Apr 15 '17 at 09:44
  • 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) – MC Emperor Apr 15 '17 at 10:06

2 Answers2

1

I found some errors in your code which might be giving NullPointerException So change it accordingly.

First mistake is here:

Node root= head;
if(head==null){
    root.data=data;
    root.next=null;
    root.prev=null;
}

So here you need to first create an object of Node class and assign it to root so code will look like :

Node root= head;
if(head==null){
    root=new Node();
    root.data=data;
    root.next=null;
    root.prev=null;
}

Another Mistake I encountered is in condition of if(head.data<data && head.next.data>data && head.next!=null). Here you are should validate head.next before accessing it in head.next.data. Suppose if head.next is null then the evaluation of condition of loop goes like this.

1) head.data<data so suppose this return true so we will check next condition.

2) head.next.data>data now if head.next is null then here condition would be null.data which will throw an NullPointerException. So here you should also check that head.next is not null. You are doing this is next condition but it is getting executed before validation it.

So here you just need to change order of the condition of if statement like: if(head.data<data && head.next!=null && head.next.data>data).

This will solve your problem.

Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
0
 Node root= head;
if(head==null){
    root.data=data;

here you are trying to set data for a null object you should allocate memory for root first e.g.

head = new Node();
root = head
//then continue your code
Alnour Alharin
  • 320
  • 3
  • 13
  • i did Node root= new Node(); root= head; still facing same issue. –  Apr 15 '17 at 09:22
  • may i ask why i cant set data to null object. the object values are null right? i can change to some integer...right? –  Apr 15 '17 at 09:24