Why does java.util.ArrayList allow to add null?

Posted by Alfredo O on Programmers See other posts from Programmers or by Alfredo O
Published on 2012-09-03T14:30:44Z Indexed on 2012/09/03 15:50 UTC
Read the original article Hit count: 275

Filed under:

I wonder why java.util.ArrayList allows to add null. Is there any case where I would want to add null to an ArrayList?

I am asking this question because in a project we had a bug where some code was adding nulls to the List and it was hard to spot where the bug was. Obviously a NullPointerException was thrown but not until another code tried to access the element. The problem was how to locate the code that added the null object. It would have been easier if the ArrayList throwed an exception in the code where the elements was being added.

© Programmers or respective owner

Related posts about java