title: Condition always true when reached in while loop
body: <p>I created a method to insert a Linked List node in the correct place of a sorted (increasing) linked list but I am having some problems.</p>
  
  <pre><code>public static void sortedInsert(LinkedListNode root, int value) {
          LinkedListNode newNode = new LinkedListNode(value);
          if (root == null || root.data &gt;= value) {
              newNode.next = root;
          } else {
              LinkedListNode curr = root;
              while (curr.next.data &lt; value &amp;&amp; curr.next != null) {
                  curr = curr.next;
              }
              newNode.next = curr.next;
              curr.next = newNode;
          }
      }
  </code></pre>
  
  <p>errors:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at LinkedLists.LinkedListProblems.sortedInsert(LinkedListProblems.java:188)
  </code></pre>
  
  <p>The <code>curr.next != null</code> portion is highlighted in intellij so I'm assuming that is causing the error. This error only occurs when the value I added is bigger than the last value of the sorted linked list</p>
  
  <p>However when the iterate to the last node of the linked list and the value of that node is still less than the value of the parameter. Shouldn't that exit the while loop?</p>