title: Java Singly-Linked-List Recreation
body: <p>So for an assignment I need to basically make a singly linked list, with 2 methods, <code>add(x)</code> This adds a new node to the end of the list, and a <code>deleteMin()</code> that removes from the end of the list</p>
  
  <p>At the bottom is the code I made. I keep on getting the error <code>java.lang.NullPointerException</code> at line 66</p>
  
  <p>Inside the add(x) method at <code>head.next = u;</code></p>
  
  <p>I have tried to change the code a few times to fix this error and nothing seems to work.</p>
  
  <pre><code>package assignment1;
  
  import java.util.InputMismatchException;
  import java.util.Scanner;
  
  public class SLL {
      private int item;
      private SLL next;
  
      SLL(int x, SLL y){
          item = x;
          next = y;
      }
  
      public static SLL head;
      public static SLL tail;
      public static int n;
  
      public static void main(String[] args){
          boolean x = true;
          Scanner user_input = new Scanner(System.in);
          System.out.println("A = add element, R = remove element, L = to list items inside Singly-Linked-List, Q = to stop session");
          while (x == true) {
              String user = user_input.next();
              String userLow = user.toLowerCase();
              switch (userLow) {
                  case "a":
                      System.out.println("Add an integer");
                      int a;
                      try {
                          a = user_input.nextInt();
                      } catch (InputMismatchException e) {
                          System.out.println("Incorect input, please input an integer.");
                          break;
                      }
                      System.out.println("");
                      add(a);
                      break;
                  case "r":
                      deleteMin();
                      break;
                  case "l":
                      //list();
                      break;
                  case "q":
                      x = false;
                      break;
                  case "help":
                      System.out.println("A = add element, R = remove element, L = to list items inside queue, Q = to stop session");
                      break;
                  case "?":
                      System.out.println("A = add element, R = remove element, L = to list items inside queue, Q = to stop session");
                      break;
                  default:
                      System.out.println("Not a recognized command: try 'help' or '?' for a list of commands");
                      break;
              }
          }
      }
  
      public static void add(int x){
          SLL u = new SLL(x, head);
          if (n == 0) {
              head.next = u; // &lt;&lt;&lt;------------
              head.next.next = tail;
          } else {
              SLL current = head;
              for (int i = 0; i &lt; n; i++) {
                  current = current.next;
              }
              current.next = u;
              current.next.next = tail;
          }
          n++;
      }
      public static void deleteMin(){
          if (n == 0) {
              System.out.println("No elements inside list");
          } else if (n == 1) {
              head.next = null;
              tail.next = null;
              n--;
          } else {
              head.next = head.next.next;
              head.next.next = null;
              n--;
          }
      }
  }
  </code></pre>