title: Java.lang.NullPointerException error. Checking for a null object
body: <p>So what I'm trying to do here is to check if an object exists or not through <code>if(x.next==null)</code> and I'm getting an error for that which won't let me access a null object. I also get the same error when trying to modify an object for e.g. <code>x.next=y</code></p>
  
  <p>The code is simple, it's just to implement a linked list. 
  Thanks!</p>
  
  <pre><code>//import SingleLinkedList1.Node;
  
  public class SingleLinkedList2 implements ISimpleList2 {
  
  private class Node
  {   int    value;
      Node   next; }
  
  private Node first;
  private Node last;    
  
  
  public void insertFront(int item) {
      // TODO Auto-generated method stub
      Node oldfirst = first;
  
      // Create the new node
      Node newfirst = new Node();
      newfirst.value = item;
      newfirst.next = oldfirst;
  
      // Set the new node as the first node
      first = newfirst;
      if(oldfirst.next==null){
          last=first;
      }
  }
  
  public int removeFront() {
      // TODO Auto-generated method stub
      // Save the previous first
      Node oldfirst = first;
  
      if(oldfirst.next==null){
          last=null;
      }
  
      // Follow the first's node (possibly empty)
      // and set the first to that pointer
      first = oldfirst.next;
  
      // Return the value of old first 
      return oldfirst.value;
  
  }
  
  public void insertEnd(int item) {
      // TODO Auto-generated method stub
  
  
      Node newLast=new Node();
      newLast.value=item;
      last.next=newLast;
  
      last=newLast;
  
  }
  
  public int removeEnd() {
      // TODO Auto-generated method stub
      Node oldLast=last;
      Node check=new Node();
      check=first;
      while(check.next!=last ){
          check=check.next;
      }
      last=check;
  
      return oldLast.value;
  
  }
  
  public boolean isEmpty()
  {
      if(first.next==null){
          return true;
      }
      else{
          return false;
      }
  }
  
  
  }
  </code></pre>