title: How do I fix the NullPointerException with my Deque class which uses doubly linked lists?
body: <p>Edit: This question seems to have been more confusing to answer than I intended it to be. But someone managed to figure it out.</p>
  
  <p>I'm getting a NullPointerException from the Deque class and I was wondering if anyone could walk me through what is going on here? This assignment and the principles are rather new to me so I'm struggling to figure out if I have accidentally interpreted one of the concepts incorrectly.</p>
  
  <p>Adding some more information shortly:</p>
  
  <p>The Output on Netbeans:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at deque.Deque.insertRight(Deque.java:46)
      at deque.DequeTester.main(DequeTester.java:25)
  Java Result: 1
  </code></pre>
  
  <p>Also, every DoublyLinkedList in "myList" is a null value. All ten of them.</p>
  
  <p>The main:</p>
  
  <pre><code>public class DequeTester
  {
      public static void main(String[] args) {
  
          // Declare and instantiate a new list-based Deque
          Deque myDeque = new Deque(10);
          // See Assignment 3 requirements for setting up the test
  
         long iter = 1;
  
          for(int i = 0; i &lt; 10; i++)
          {
              if(i % 2 == 1)
              {
                  myDeque.insertLeft(iter);
              }
              else
              {
                  myDeque.insertRight(iter);  //exception from here to Deque class
              }
              iter++;
          }
          //additional code
      }
  }
  </code></pre>
  
  <p>This is the Deque class I had to fill in for the most part:</p>
  
  <pre><code>public class Deque
  {
  
      // TO-DO: Instance variable(s) go here
      private int left;
      private int right;
      private DoublyLinkedList[] myList;
      private int maxSize;
      private int numElems;
  //--------------------------------------------------------------
  
      public Deque(int size)         // constructor
      {
          // TO-DO
          numElems = 0;
          maxSize = size;
          myList = new DoublyLinkedList[maxSize];
          left = (maxSize / 2) + 1;
          right = maxSize / 2;
      }
  //--------------------------------------------------------------
  
      public void insertLeft(long j) // put item at left of deque
      {
         // TO-DO
          if(left == 0)
          {
              left = maxSize + 1;
          }
          left--;
          myList[left].insertFirst(j);
          numElems++;
      }
  //--------------------------------------------------------------
  
      public void insertRight(long j) // put item at right of deque
      {
          // TO-DO
          if(right == maxSize)
          {
              right = -1;
          }
          right++;
          myList[right].insertLast(j); //Exception is thrown on this line.
          numElems++;
      }
  //--------------------------------------------------------------
  
      public long removeLeft()      // take item from left of deque
      {
          // TO-DO
          long temp; 
          Link linkTemp = myList[left++].deleteLast();
          temp = linkTemp.dData;
          if(left == 0)
          {
              left = maxSize;
          }
          numElems--;
          return temp;
      }
  //--------------------------------------------------------------
  
      public long removeRight()    // take item from right of deque
      {
          // TO-DO
          long temp; 
          Link linkTemp = myList[right--].deleteFirst();
          temp = linkTemp.dData;
          if(right == maxSize)
          {
              right = 0;
          }
          numElems--;
          return temp;
      }
  //--------------------------------------------------------------
  
      public boolean isEmpty()     // true if deque is empty
      {
          // TO-DO
          return numElems == 0;
      }
  //--------------------------------------------------------------
  
      public void display()
      {
          // TO-DO
          for(int i = 0; i &lt; numElems; i++)
          {
              myList[i].displayForward();
          }
  
          if(numElems != 0)
          {
              if(right &lt; left)
              {
                  for(int i = right; i &lt; left; i++)
                  {
                      myList[i].displayForward();
                  }
              }
              else
              {
                  for(int i = left; i &lt; numElems; i++)
                  {
                      myList[i].displayForward();
                  }
                  for(int i = 0; i &lt; right; i++)
                  {
                      myList[i].displayForward();
                  }
              }
          }
      } 
  //--------------------------------------------------------------
  }
  </code></pre>
  
  <p>This is the DoublyLinkedList class that was provided for the assignment:</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package deque;
  
  public class DoublyLinkedList
     {
     private Link first;               // ref to first item
     private Link last;                // ref to last item
  // -------------------------------------------------------------
     public DoublyLinkedList()         // constructor
        {
        first = null; //was null                  // no items on list yet
        last = null;  //was null
        }
  // -------------------------------------------------------------
     public boolean isEmpty()          // true if no links
        { return first==null; }
  // -------------------------------------------------------------
     public void insertFirst(long dd)  // insert at front of list
        {
        Link newLink = new Link(dd);   // make new link
  
        if( isEmpty() )                // if empty list,
           last = newLink;             // newLink &lt;-- last
        else
           first.previous = newLink;   // newLink &lt;-- old first
        newLink.next = first;          // newLink --&gt; old first
        first = newLink;               // first --&gt; newLink
        }
  // -------------------------------------------------------------
     public void insertLast(long dd)   // insert at end of list
        {
        Link newLink = new Link(dd);   // make new link
        if( isEmpty() )                // if empty list,
           first = newLink;            // first --&gt; newLink
        else
           {
           last.next = newLink;        // old last --&gt; newLink
           newLink.previous = last;    // old last &lt;-- newLink
           }
        last = newLink;                // newLink &lt;-- last
        }
  // -------------------------------------------------------------
     public Link deleteFirst()         // delete first link
        {                              // (assumes non-empty list)
        Link temp = first;
        if(first.next == null)         // if only one item
           last = null;                // null &lt;-- last
        else
           first.next.previous = null; // null &lt;-- old next
        first = first.next;            // first --&gt; old next
        return temp;
        }
  // -------------------------------------------------------------
     public Link deleteLast()          // delete last link
        {                              // (assumes non-empty list)
        Link temp = last;
        if(first.next == null)         // if only one item
           first = null;               // first --&gt; null
        else
           last.previous.next = null;  // old previous --&gt; null
        last = last.previous;          // old previous &lt;-- last
        return temp;
        }
  // -------------------------------------------------------------
     public void displayForward()
        {
        System.out.print("List (first--&gt;last): ");
        Link current = first;          // start at beginning
        while(current != null)         // until end of list,
           {
           current.displayLink();      // display data
           current = current.next;     // move to next link
           }
        System.out.println("");
        }
  // -------------------------------------------------------------
     }
  </code></pre>
  
  <p>and the Link class that was provided, if you need it:</p>
  
  <pre><code>public class Link
     {
     public long dData;                 // data item
     public Link next;                  // next link in list
     public Link previous;              // previous link in list
  // -------------------------------------------------------------
     public Link(long d)                // constructor
        { dData = d; }
  // -------------------------------------------------------------
     public void displayLink()          // display this link
        { System.out.print(dData + " "); }
  // -------------------------------------------------------------
     }
  </code></pre>