1

I have a task to make my own Dynamic Array without using any ArrayLists or other pre-made classes for Arrays in java. I get an Exception in thread "main" java.lang.NullPointerException error when I try to add my object (for example it's Integer).

Dinaminis<Integer> array = new Dinaminis<>();
array.ideti(5);

And my Dinaminis class looks like this:

public class Dinaminis<T> implements DArray<T> 
{    
    private Object[] array;
    private int kiek;
    private Object[] temp;

    public Dinaminis() {
        array = new Object[10];
        kiek = 0;
    }

    @Override
    public void ideti(Object o) {
        if (array.length == kiek) {
            temp = new Object[kiek*2];
        }
        for (int i=1; i < kiek; i++){
            temp[i] = array[i];
        }
        array = temp;
        array[kiek] = o;
        kiek++;
    }
}

The thing is when I want to use "ideti" method, I want to check if the array is full, and if it's full it should create a double sized array. But I get an error even before my array is full.

2
  • What if array.length == kiek is false? Commented Nov 30, 2014 at 10:54
  • For starters, you shouldn't make temp an instance variable (just local to the method), and System.arraycopy is a lot faster than hand-copying. Commented Nov 30, 2014 at 11:43

3 Answers 3

3

Your ideti method is wrong. This should work:

public void ideti(Object o) {
    if (array.length == kiek) {
        temp = new Object[kiek*2];
        // only copy old array to new array when old one is full
        for (int i=0; i < kiek; i++){ // index starts at 0, not 1
            temp[i] = array[i];
        }
        array = temp;
    } 
    array[kiek] = o;
    kiek++;
}

Your problem was that the loop that copies the old array to the new one was executed even when the original array wasn't full, in which case temp wasn't initialized, which led to NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest you store your array as your it's generic type T. I would pass in to the Class<T> to the Dinaminis constructor, then you can use Array.newInstance(). Also, I'd prefer to use System.arraycopy() and override toString() like

public class Dinaminis<T> implements DArray<T> {
    private T[] array;
    private int kiek;
    private Class<T> cls;

    public Dinaminis(Class<T> cls) {
        this.cls = cls;
        array = (T[]) Array.newInstance(cls, 10);
        kiek = 0;
    }

    @Override
    public void ideti(T o) {
        if (kiek == array.length) {
            T[] temp = (T[]) Array.newInstance(cls, array.length * 2);
            System.arraycopy(array, 0, temp, 0, array.length);
            array = temp;
        }
        array[kiek++] = o;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("len = %d [", array.length));
        for (int i = 0; i < kiek; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append("]");
        return sb.toString();
    }
}

Then you could test it with something like

public static void main(String[] args) {
    DArray<Integer> t = new Dinaminis<Integer>(Integer.class);
    for (int i = 0; i < 11; i++) {
        t.ideti(i);
    }
    System.out.println(t);
}

Output is

len = 20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Obviously len was added to this output for pedagogical reasons, and in real code I probably wouldn't display it (except perhaps in a log message).

Comments

0

You need to initialize your temp array before using it.

You do initialize it inside of the if but not outside the if

temp = new Object[kiek*2];

so allocation of the double sized array should only happen when the size of the array is complete

if (array.length == kiek)  //check if array size is reached
{ 

  temp = new Object[kiek*2]; //allocate a double sized array

  for (int i=1; i < kiek; i++) //copy elements - are you sure you don't want to start with i=0? You won't copy the first element if you start at 1
  {
    temp[i] = array[i];
  }
  array = temp;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.