문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top