0

Whenever I try one of the push methods, it just gives me NullPointerException... I don't understand why, For example if i pushLeft(1) as a first method after creating an array of 4, doesn't (0+0)%4 = 0, so items[0] would become c??

import CITS2200.*;

public class DequeCyclic<E> implements Deque<E>
{
    private E[] items;
    private int header;
    private int maxSize;
    private int count;

    public DequeCyclic(int s)
    {
        @SuppressWarnings("unchecked")
        E[] items = (E[]) new Object[s];
        count = 0;
        header = 0;
        maxSize = s;
    }

    public boolean isEmpty()
    {
        return count == 0;
    }

    public boolean isFull()
    {
        return count == maxSize;
    }

    public void pushRight(E c) throws Overflow
    {
        if (!isFull())
        {
            header = (header-1+maxSize)%maxSize;
            count++;
            items[header] = c;
        }
        else throw new Overflow("deque is full.");
    }

    public void pushLeft(E c) throws Overflow
    {
        if (!isFull())
        {
            items[(header+count)%maxSize] = c;
            count++;
        }
        else throw new Overflow("deque is full");
    }

EDIT I added additional methods because someone commented that the problem isn't in the posted code, however I don't see how any of these methods could cause a NullPointerException...

 public E peekRight() throws Underflow
{
    if (!isEmpty())
        return items[header];
    else throw new Underflow("deque is empty");
}    

public E peekLeft() throws Underflow
{
    if (!isEmpty())
        return items[(header+count)%maxSize];
    else throw new Underflow("deque is empty");
}

public E popRight() throws Underflow
{
    if (!isEmpty())
    {
        E temp = items[header];
        items[header] = null;
        header = (header+1)%maxSize;
        count--;
        return temp;
    }
    else throw new Underflow("deque is empty");
}

public E popLeft() throws Underflow
{
    if (!isEmpty())
    {
        count--;
        E temp = items[(header+count)%maxSize];
        items[(header+count)%maxSize] = null;
        return temp;
    }
    else throw new Underflow("deque is empty");
}

}

  • your problem is not in the currently posted code, but at or near the point where you call pushLeft/pushRight. The object you are calling the method on is likely null. – Andi Mar 21 '21 at 07:21
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Andi Mar 21 '21 at 07:22
  • @Andi I edited to add the rest of my code, I don't see how it could cause the problem though, I've already read that post but regardless I am lost – Lewis Hellewell Mar 21 '21 at 07:34
  • your exceptions happens where you call the the methods, outside of the ````DequeCyclic```` class. If the problem was with the array, it would be an ````ArrayIndexOutOfBoundsException```` since the array is assigned in the constructor. – Andi Mar 21 '21 at 07:46
  • Please post the usage of the Deque – EricSchaefer Mar 21 '21 at 09:09
  • @Tom That article solved the issue, thank you, issue was I was shadowing the array. – Lewis Hellewell Mar 22 '21 at 01:49
  • https://stackoverflow.com/questions/30567802/why-does-java-throw-nullpointerexception-here soved the issue – Lewis Hellewell Mar 22 '21 at 01:50

0 Answers0