Question

I'm trying to add all the elements of an ArrayList into another ArrayList,

I've tried using

if (!listTwo.isEmpty()){
    finalList.addAll(listTwo);
    }

finalList.addAll(listTwo);

However, this keeps sending me an NullPointerException error. Both are ArryList, and listTwo does have elements inside of it.

Any idea why it's sending this exception? Thanks a lot

Was it helpful?

Solution

finalList.addAll(listTwo);

Will add the elements from listTwo into finalList.

If you are getting a NullPointerException it is because one of your lists is null. Note that isEmpty() will throw a NullPointerException if the list is null, so that probably won't help you.

To check if the lists are null, do listTwo == null and/or finalList == null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top