-1

Edit: This question seems to have been more confusing to answer than I intended it to be. But someone managed to figure it out.

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.

Adding some more information shortly:

The Output on Netbeans:

Exception in thread "main" java.lang.NullPointerException
    at deque.Deque.insertRight(Deque.java:46)
    at deque.DequeTester.main(DequeTester.java:25)
Java Result: 1

Also, every DoublyLinkedList in "myList" is a null value. All ten of them.

The main:

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 < 10; i++)
        {
            if(i % 2 == 1)
            {
                myDeque.insertLeft(iter);
            }
            else
            {
                myDeque.insertRight(iter);  //exception from here to Deque class
            }
            iter++;
        }
        //additional code
    }
}

This is the Deque class I had to fill in for the most part:

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 < numElems; i++)
        {
            myList[i].displayForward();
        }

        if(numElems != 0)
        {
            if(right < left)
            {
                for(int i = right; i < left; i++)
                {
                    myList[i].displayForward();
                }
            }
            else
            {
                for(int i = left; i < numElems; i++)
                {
                    myList[i].displayForward();
                }
                for(int i = 0; i < right; i++)
                {
                    myList[i].displayForward();
                }
            }
        }
    } 
//--------------------------------------------------------------
}

This is the DoublyLinkedList class that was provided for the assignment:

/*
 * 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 <-- last
      else
         first.previous = newLink;   // newLink <-- old first
      newLink.next = first;          // newLink --> old first
      first = newLink;               // first --> 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 --> newLink
      else
         {
         last.next = newLink;        // old last --> newLink
         newLink.previous = last;    // old last <-- newLink
         }
      last = newLink;                // newLink <-- 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 <-- last
      else
         first.next.previous = null; // null <-- old next
      first = first.next;            // first --> 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 --> null
      else
         last.previous.next = null;  // old previous --> null
      last = last.previous;          // old previous <-- last
      return temp;
      }
// -------------------------------------------------------------
   public void displayForward()
      {
      System.out.print("List (first-->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("");
      }
// -------------------------------------------------------------
   }

and the Link class that was provided, if you need it:

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 + " "); }
// -------------------------------------------------------------
   }
Crazierinzane
  • 71
  • 1
  • 1
  • 8
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Pokechu22 Nov 07 '14 at 00:42
  • I did use debugger, and due to inexperience I could not fix the problem. – Crazierinzane Nov 07 '14 at 00:42
  • I also looked at that post already. I did my research for an answer but I'm stuck. – Crazierinzane Nov 07 '14 at 00:43
  • What line do you get the exception? When you create the object or when you are trying to insertleft or insertright? – Jay Nov 07 '14 at 00:48
  • @Jay I marked it in the Deque class. The exception is thrown in the insertRight method when it calls the insertLast method of the DoublyLinkedList. – Crazierinzane Nov 07 '14 at 00:51
  • @LeleDumbo Reported for being useless and condescending when he could have actually tried or not posted obstructive garbage. – Crazierinzane Nov 07 '14 at 01:32
  • @LeleDumbo Once again reported. Please refrain from spamming in the future. – Crazierinzane Nov 08 '14 at 23:51

1 Answers1

2

I think your problem starts with this:

private DoublyLinkedList[] myList;
...
myList = new DoublyLinkedList[maxSize];

What you're doing here is declaring an array of maxSize DoublyLinkedLists, whereas what you want is a single DoublyLinkedList that you store the elements of the deque into.

Squirrel
  • 2,262
  • 2
  • 18
  • 29
  • I thought that might be it, and that's how I started off, but the assignment said to base the code off a Deque class as if I were using an array instead of a DoublyLinkedList. Then, when I tried with just one DoublyLinkedList I got to the insertLeft method and didn't know how to proceed without it also being an array. – Crazierinzane Nov 07 '14 at 00:57
  • 1
    Well, regardless of the assignment wording or how you want to implement it (although I don't understand how an array of lists would implement a deque), my point is that this is probably what's causing your exception. The statement `new DoublyLinkedList[maxSize]` creates an array of lists, but each of them starts as `NULL`, when you later call `myList[right].insertLast(j)` in your `insertRight` function, `myList[right]` is still `NULL`. – Squirrel Nov 07 '14 at 01:21
  • You are correct. Reworking to code to simply not use an array of lists did fix what I was experiencing and the code is working in one respect now. Since this answers the question I'm not going to troubleshoot the rest of the issue on this question, which is getting the deque and the list to print. – Crazierinzane Nov 07 '14 at 01:26