title: Sorted Insert into Singly Linked List from scratch
body: <p>I have been trying to create a sorted linked list from scratch that takes only strings, but sorts it upon insertion. Here is my current code: </p>
  
  <pre><code>import java.util.Arrays;
  
  public class SortedLinkedList {
  
      private StringNode head = null;
  
      /**
       * Default Constructor for a sorted linked list
       */
      public SortedLinkedList() {}
  
      /**
       * Will add a new node with the specified data to the correctly sorted 
       * position in the list
       */
      public void add(String data) {
  
          if (head == null) {
              head = new StringNode(data);
          }
  
          StringNode temp = new StringNode(data);
          StringNode current = head;
  
          if (current != null) {
              while (current.getNext() != null) {
                  if (current.getData().toString().compareTo(current.getNext().getData().toString()) &lt; 0) {
                      temp.setNext(current.getNext());
                      current.setNext(temp);
                  } else
                      current.setNext(temp);
              }
          }
      }
  
      /**
       * Will remove the node that matches the specified data from the list.
       * Returns true if node is found, otherwise will return false
       */
      public boolean remove(String data) {
          StringNode current = head;
  
          if (head != null) {
  
              while (current.getNext() != null) {
  
                  if (current.getData().toString().equals(data)) {
                      current.setNext(current.getNext().getNext());
                      return true;
                  }
  
                  current = current.getNext();
              }
          }
          return false;
      }
  
      /**
       * Will cycle through the list and display each item on a new line
       */
      public void display() {
  
          if (head != null) {
  
              StringNode current = head.getNext();
              System.out.print("[");
  
              while (current.getNext() != null) {
                  System.out.print(current.getData().toString() + ", ");
                  current = current.getNext();
              }
  
              System.out.print("]");
          }
      }
  
      // Inner Class 
  
      class StringNode {
  
          String data;
          StringNode next;
  
          public StringNode(String nodeData) {
              next = null;
              data = nodeData;
          }
  
          /**
           * Getter of Data
           */
          public String getData() {
              return data;
          }
  
          /**
           * Getter of Next
           */
          public StringNode getNext() {
              return next;
          }
  
          /**
           * Setter of Data
           */
          public void setData(String newData) {
              data = newData;
          }
  
          /**
           * Setter of Next
           */
          public void setNext(StringNode nextNode) {
              next = nextNode;
          }
      }
  
  }
  </code></pre>
  
  <p>Ive been able to add without insertion sorting, however, after I tried to code in insertion coding it broke. It seems like it no longer adds any values. My current output from my driver:</p>
  
  <pre><code>public class Driver {
      public static void main(String[] args) {
          SortedLinkedList name = new SortedLinkedList();
  
          name.add("v");
          name.add("a");
          name.add("b");
  
          name.display();
      }
  }
  </code></pre>
  
  <p>Is a null pointer exception in my display method, line 70, which is the creation of my while loop. I am completely lost and need some guidance. Thanks.</p>