Question: We're getting this error

```
java.lang.NullPointerException
    at java.util.ArrayList.<init>(Unknown Source)
    at de.mystuff.ExtendedArrayList.<init>(ExtendedArrayList.java:38)

```

where ExtendedArrayList:38 is

```
new ArrayList(new ArrayCollection<E>(data));

```

In short: the ArrayList constructor sometimes seems to choke on our home grown Collection implementation ArrayCollection.

I was unable to reproduce it on my computer even with the exact same version that was distributed to our customers.

But i'm not 100% sure that they are using the JRE we included.
So, i googled for some ArrayList.java source code and found openJDK 6b17 which has this

```
public ArrayList(Collection<? extends E> c) {
    elementData = c.toArray();
    size = elementData.length;
    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if (elementData.getClass() != Object[].class)
        elementData = Arrays.copyOf(elementData, size, Object[].class);
}

```

That would make sense, because if there's no data our *ArrayCollection.toArray()* returns **null**. And this constructor looked safe (and worked without exception) for the 1.5.0\_09 Sun JDK/JRE implementation we are using.  

But openJDK only seems to release for the unix world. Is this code also part of a Windows JRE? And if so, which version?

NB: I know that we have to fix our classes, but i want to make sure i understand the cause of the NullPointerException.

Answer: 
I don't think you specified exactly which JRE version/vendor you are using, but here is the source for the constructor in question for Sun JDK 1.6.0\_17 (if you aren't already aware, the source for most classes in the `java` namespace ships with the JDK):

```
public ArrayList(Collection<? extends E> c) {
    elementData = c.toArray();
    size = elementData.length;
    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if (elementData.getClass() != Object[].class)
         elementData = Arrays.copyOf(elementData, size, Object[].class);
}

```

*(the comment is the author's, not mine)*

This should pretty clearly show the potential cause of a NPE in this constructor: if `c` is null or (in your case) if `c.toArray()` returns null.
The List#toArray method should never return null. Your implementation of ArrayCollection may have overriden the toArray methods and may be returning null for empty collections instead of a new empty array.

And yes, it is possible that different JRE's behave differently. Some may tolerate null instead of an array while others don't.

I'd fire up my [Java decompiler](http://java.decompiler.free.fr/) and have a look at the actual implementation of this ArrayList constructor.