title: java - sorted list recursive insert method
body: <p>I'm implementing an abstract class, and implementing an <code>insert</code> method which inserts into a sorted list. This is the recursive method I have so far:</p>
  
  <pre><code>public void insert(E data) {
      Node&lt;E&gt; temp = head;
      insert(temp, data);
  }
  
  private void insert(Node&lt;E&gt; curr, E data){
      Node&lt;E&gt; temp = new Node&lt;E&gt;(data);
      if (head == null || data.compareTo(head.data) &lt; 0) {
          temp.next = head;
          head = temp;
      }
      else if(curr.data.compareTo(data) &lt; 0 &amp;&amp; (curr.next == null || curr.next.data.compareTo(data) &gt; 0)){
          temp.next = curr.next;
          curr.next = temp;
          return;
      }
      else{
        insert(curr.next, data);
      }
  }
  </code></pre>
  
  <p>However, anytime I try to insert 2+ items into the list, I get a null pointer exception error. Can someone explain my mistake? This is what happens when I try to run it simply inserting 1 and 2: <a href="https://gyazo.com/d254d563675b9d1b0efbce443eda4445" rel="nofollow">https://gyazo.com/d254d563675b9d1b0efbce443eda4445</a></p>