4

When ever I add an object to this ArrayList, my resize method, gives me a NullPointerException. The list is initialized with a size of 1, and the first element is added to possition 0 in the array.

Here is my arrayList AKA DynamicArray

//Implementation of a dynamic array
// Add remove methods


public class DynamicArray {
    private Object[] data;
    private int size;

public void DynamicArray(){
    data = new Object[1];
    size = 0;
}

public int size(){return size;}

public Object get(int index){return data[index];};

private void resizeIfFull()
{
    if (size < data.length){
        return;
        } else {
            Object[] bigger = new Object[2 * data.length];
            for (int i = 0; i < data.length; i++){
                bigger[i] = data[i];
                data = bigger;
        }
    }
}

public void add(Object obj){
    resizeIfFull();
    data[size] = obj;
    size++;
}

public void add(int index, Object obj){
    resizeIfFull(); 
    for(int i = size - 1; i >= index; i--){
        data[i+1] = data[i];
    }
    data[index] = obj;
    size++;
}

public void remove(int index){
    for(int i = index; i < size; i++){
        data[i] = data[i+1];
    }
    size--;
}

}

Here is my testing class.

public class AlgorTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    DynamicArray dynam = new DynamicArray();
    System.out.println(dynam.size());
    dynam.add("first");

}

}

Here is my output from the testing class.

0
Exception in thread "main" java.lang.NullPointerException
at DynamicArray.resizeIfFull(DynamicArray.java:20)
at DynamicArray.add(DynamicArray.java:38)
at AlgorTest.main(AlgorTest.java:8)
Ryan Miles
  • 307
  • 4
  • 11
  • 3
    At which line do you get the NPE? –  Jan 25 '18 at 14:54
  • 2
    `data = bigger;` should be done after the loop. – Pshemo Jan 25 '18 at 14:56
  • 3
    Remove void type on constructor method –  Jan 25 '18 at 14:57
  • 1
    You may start working with an IDE, it will immediately highlight such annoying time-wasting mistakes (and also auto-format your code). – Zabuzard Jan 25 '18 at 14:58
  • That duplicate doesn't really address the problem. It explains what NPE is and OP might have known that already - the problem is the typo. So imo a different close reason would have been more appropriate. – Zabuzard Jan 25 '18 at 15:01
  • 1
    Previous duplicate may be general but still explained what problem was about (but not necessarily cause of it in this specific case). Instead of reopening it I would suggest adding more specific question like [java “void” and “non void” constructor](https://stackoverflow.com/q/24963718) to duplicate list (can't do it now since can't cast multiple close votes on same question). – Pshemo Jan 25 '18 at 15:06
  • @Zabuza You are right, eclipse did show me a warning. I missed it. – Ryan Miles Jan 25 '18 at 15:27

2 Answers2

6

Confusingly, this isn't a constructor:

public void DynamicArray(){
    data = new Object[1];
    size = 0;
}

It's a function called DynamicArray (very confusing, I know).

Without the class having a constructor, data remains null and leads to an NPE when you try to access the array.

Drop the void keyword to turn the function into a constructor (which would then initialize data etc):

public DynamicArray(){
    data = new Object[1];
    size = 0;
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
6

constructor doesn't have return value , remove return type from constructor (void)

public DynamicArray(){
    data = new Object[1];
    size = 0;
}

in your case when you initialize object from DynamicArray class then default constructor will execute which does nothing

Ali Faris
  • 17,754
  • 10
  • 45
  • 70