Skip to main content

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

I am asking this question because in a project we had a bug where some code was adding nullsnull to the ListArrayList and it was hard to spot where the bug was. Obviously a NullPointerExceptionNullPointerException was thrown but not until anotherother code tried to access the element. The problem was how to locate the code that added the nullnull object. It would have been easier if the ArrayList throwedArrayList threw an exception in the code where the elements was being added.

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.

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 null to the ArrayList and it was hard to spot where the bug was. Obviously a NullPointerException was thrown but not until other 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 ArrayList threw an exception in the code where the elements was being added.

Question Protected by gnat
edited tags
Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
edited title
Link
Alfredo Osorio
  • 906
  • 1
  • 11
  • 18

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

Tweeted twitter.com/#!/StackProgrammer/status/242638344738205698
Source Link
Alfredo Osorio
  • 906
  • 1
  • 11
  • 18
Loading