-5

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;
        }
idelara
  • 1,786
  • 4
  • 24
  • 48
  • I know the concept behind NullPointerException. The thing is that I cant spot why is it doing it in my code. – idelara Feb 21 '15 at 15:38
  • 1
    If you know the concept behind NPE you should know how to find the origin of exception (null object). You didn't post exception details (stack trace, line of code, null object), so it isn't really possible to help you. Please read answers to the linked question again. – default locale Feb 21 '15 at 15:44
  • [This answer](http://stackoverflow.com/a/24347569/451518) deals with NPE troubleshooting. – default locale Feb 21 '15 at 15:50
  • Tell me if I am correct or not, I think I might have found the issue. I think it is because on the test I am trying to perform the `for(int x : list)` chunk of code is actually for objects (uses an Iterator) and in my test for loop I am inserting primitive data (Int) not an actual Integer object. Right? – idelara Feb 21 '15 at 15:53
  • 1
    @JackGal Post your full stack trace and indicate the line of the code that causes the null pointer exception. – iRuth Feb 21 '15 at 15:58
  • the line `for(int x : list)` . Sorry for the question but how do I see the stack trace in Eclipse? – idelara Feb 21 '15 at 16:13
  • Stack Trace: "ERROR java.lang.NullPointerException java.lang.NullPointerException at P1Tester.runTests(P1Tester.java:23) at P1Tester.(P1Tester.java:14) at P1Tester.main(P1Tester.java:117)" – idelara Feb 21 '15 at 16:53

2 Answers2

2
for(int i = 1; i <= currentSize; i++)
  list[i] = valuesHolder[i];

This part is the culprit. You have to copy contents of 'list' to newly created array 'valuesHolder'. Instead you are doing the other way, which results in 'null' entries. The change you have to make is this:

for(int i = 1; i <= currentSize; i++)
  valuesHolder[i] = list[i];
Ramsharan
  • 2,054
  • 2
  • 22
  • 26
-1

It may be this:

list[++currentSize] = obj;

try:

list[currentSize++] = obj;

Furthermore, your first element will always be empty.

Ósky F J
  • 7
  • 4
  • I am starting at index 1 on purpose. I tried the to change my code to `list[currentSize++] = obj;` and didnt fix the issue :/ – idelara Feb 21 '15 at 15:43