title: NullPointerException Error using linked lists
body: <p>I just finished up working on this program and got it to compile but it breaks after user input and gives me this: </p>
  
  <p>Please input 0 or more values at keyboard
  12 4 3 2 1</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at Search.buildList(Search.java:41)
  at Search.main(Search.java:10)
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>import java.io.*; 
  import java.util.*;
  
  public class Search { 
  public static void main(String argv[]) throws IOException { 
  
  Scanner stdin = new Scanner(System.in);
  System.out.println("Please input 0 or more values at keyboard");
  Node head = buildList();
  
  System.out.println("Now printing list");
  printList(head);
  System.out.println("\nWhat key in list are you searching for? ");
  int key = stdin.nextInt();
  System.out.print("Your key was ");
  if (search(head, key))
  System.out.println("found.");
  else
  System.out.println("not found.");
  
  }
  
  private static void printList(Node head)
  {
              if (head != null)
              {
                      System.out.print(head.getItem() + " ");
                      printList(head.getNext());
              }
  }
  
  private static Node buildList() throws IOException
  {
   // Post : Inserts 0 or more numerical values from keyboard into list
  //          using the Scanner class and returns head of list
  
  Scanner input = new Scanner(System.in);
  Node head = null;
  Node first = new Node(input.nextInt());
  head.setNext(first);
  while(input.hasNext())
  {    
  insert(first, input.nextInt());
  /*
    Node curr = new Node(input.nextInt());
    Node prev = head;
    while (true)
    {
  prev = prev.getNext();
  if ((int)curr.getItem() &lt; (int)prev.getItem())
  {
    head.setNext(curr);
    curr.setNext(prev);
    break;
  }
  if (prev.getNext() == null)
  {
    prev.setNext(curr);
    break;
  }
    }*/
  }
  return first;
  } 
  
  private static Node insert(Node head, Comparable newValue)
  {
  Node prev, curr = head;
  
  for (prev = null,  curr = head;
           curr != null &amp;&amp; newValue.compareTo(curr.getItem()) &gt; 0;
           prev = curr, curr = curr.getNext() ) {}
  
      Node newNode = new Node(newValue, curr);
  if (prev != null)
      {
          prev.setNext(newNode);
      return head;
      }
  else
      return newNode;
  }
  
  private static boolean search(Node head, Comparable key)
  {
   // PRE:  head points to the front of linked list;  list may be
   //         empty or non-empty;  key is item searching for
   // POST: returns true or false regarding whether key is found in
   //         list
  if (head == null){
      return false;}
  else if (head.getItem().equals(key)){
      return true;}
  else {
      return search(head.getNext(), key);
  }
  
  } 
  
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>The output should be similar to the following:</p>
  
  <p>Please input 0 or more values at keyboard</p>
  
  <p>12 4 -1 5 3 0 2</p>
  
  <p>Now printing list</p>
  
  <p>-1 0 2 3 4 5 12
  What key are you searching for? 15
  Your key was not found</p>