title: NullPointerException with Linked List (Deleting an element)
body: <p>I am trying to create a function to delete an element from a linked list. However, I keep getting a NullPointerException. When I run the program, it states:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at ElephantList.delete(ElephantList.java:30)
      at BabyElephantWalk.main(BabyElephantWalk.java:15)
  </code></pre>
  
  <p>How can I delete an element from the linked list without having this exception thrown? Here is the code</p>
  
  <p>BabyElephantWalk.java</p>
  
  <pre><code>public class BabyElephantWalk
  {
      public static void main (String[] args)
      {
  
          ElephantList walk = new ElephantList();
          walk.add (new Elephant("Charlie"));
          walk.add (new Elephant("Donna"));
          walk.add (new Elephant("Chilli"));
          walk.add (new Elephant("Piper"));
          walk.add (new Elephant("Ziva"));
  
          walk.delete ("Charlie");
          System.out.println (walk);
      }
  }
  </code></pre>
  
  <p>ElephantList.java</p>
  
  <pre><code>public class ElephantList
  {
  private ElephantNode head;
  private ElephantNode list;
  public ElephantList()
  {
      list = null;
  }//end ElephantList constructor
  
  public void add(Elephant cat)
  {
      ElephantNode node = new ElephantNode(cat);
      ElephantNode current;
      if (list == null)
          list = node;
      else
      {
          current = list;
          while (current.next != null)
              current = current.next;
          current.next = node;
      }
  }//end add
  
  public void delete(Object x)
  {
      {
          if (head.data == x)
          {
              head = head.next;
          }
          else
          {
              ElephantNode temp = head;
              while (temp.next != null)
              {
                  if (temp.next.data.equals(x))
                  {
                      temp.next = temp.next.next;
                      break;
                  }
                  else 
                  {
                      temp = temp.next;
                  }
              }
          }
      }
  }
  
  public String toString ()
  {
      String result = "";
      ElephantNode current = list;
      while (current != null)
      {
          result += current.elephant + "\n";
          current = current.next;
      }
      return result;
  }//end toString
  
  private class ElephantNode
  {
      public Elephant elephant;
      public ElephantNode next;
      public Object data;
      public ElephantNode(Elephant cat)
      {
          elephant = cat;
          next = null;
          data = null;
      }//end ElephantNode constructor
  }//ElephantNode
  }//end ElephantList
  </code></pre>
  
  <p>Elephant.java</p>
  
  <pre><code>public class Elephant
  {
      private String title;
      public Elephant(String newname)
      {
          title = newname;
      }//end Elephant constructor
      public String toString ()
      {
          return title;
      }
  }
  </code></pre>