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");
}
}