title: Why this linked list rear operation snippet code throws error?
body: <p>in the below snippet code contains 3 files, named main which is the main operation code, node file contains LinkedList creation and methods contains the linked list operation. In the add rear section throws error.. while execution. I have added the snippet error screshoot</p>
  <p>Main.java</p>
  <pre><code>import java.util.*; 
      public class main {
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              while (true) {
                  System.out.println(&quot;1-&gt; add front 2-&gt;add rear 3-&gt;display&quot;);
                  System.out.println(&quot;enter your choice&quot;);
                  int ch = in.nextInt();
                  methods list = new methods();
                  switch (ch) {
                      case 1: {
                          System.out.println(&quot;Enter the element to add&quot;);
                          int newAdd = in.nextInt();
                          list.addfront(newAdd);
                      }
                      break;
                      case 2: {
                          System.out.println(&quot;Enter the element to add&quot;);
                          int newAdd = in.nextInt();
                          list.addrear(newAdd);
                      }
                      case 3:
                          list.display();
                          break;
                      default:
                          System.exit(0);
      
                  }
      
      
              }
          }
      }
  </code></pre>
  <p>node.java</p>
  <pre><code>public class node {
      private int data;
      private node next;
  
      public node(int data) {
          this.data = data;
          this.next = null;
      }
  
      public int getData() {
          return data;
      }
  
      public node getNext() {
          return next;
      }
  
      public void setNext(node next) {
          this.next = next;
      }
  
      @Override
      public String toString() {
          return &quot;node{&quot; +
                  &quot;data=&quot; + data +
                  '}';
      }
  }
  </code></pre>
  <p>Methods.java</p>
  <pre><code>public class methods 
  {
  
      node head;
  
      public void addfront(int data)
      {
          node n= new node(data);
          if(head!=null)
             n.setNext(head);
          head=n;
      }
      public void addrear(int data)
      {
          node n = new node(data);
          node temp= head;
          while(temp.getNext()!=null)
          {
              temp=temp.getNext();
          }
          temp.setNext(n);
      }
  
      public void display()
      {
          node current = head;
          while(current!=null)
          {
              System.out.println(current);
              current=current.getNext();
          }
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ni3Dx.png" rel="nofollow noreferrer">Execution Output Image</a></p>