I dont know why my code is throwing this error. I have a generic array that can hold maxCapacity = 100, and if this number is reached with currentSize, the maxCapacity is doubled. When I perform the following test, it throws a NullPointerException. Why? NOTE: I START AT INDEX 1 (NOT 0) ON PURPOSE!!!!
Test:
for(int i=1; i <= 1100; i++)
list.addLast(i);
System.out.println("Should print 1 .. i");
for(int x : list)
System.out.print(x + " ");
System.out.println("\n");
Here's my code:
private E[] list;
private int maxCapacity, currentSize;
public ArrayLinearList(){
currentSize = 0;
maxCapacity = DEFAULT_MAX_CAPACITY;
list = (E[]) new Object[maxCapacity];
//list[0] = null;
}
public void addLast(E obj){
if(isFull()) //check if maxCap has been reached first
doubleArraySize();
System.out.println("Method called");
if(currentSize == maxCapacity - 1)
doubleArraySize();
list[++currentSize] = obj;
}
....
private boolean isFull(){
if(currentSize == maxCapacity)
return true;
return false;
}
private void doubleArraySize(){
maxCapacity *= 2;
System.out.println("doubling");
E[] valuesHolder = (E[]) new Object[maxCapacity];
for(int i = 1; i <= currentSize; i++)
list[i] = valuesHolder[i];
list = valuesHolder;
}