Say I have a List
and I know that I never want to add null
to it. If I am adding null to it, it means I'm making a mistake. So every time I would otherwise call list.add(item)
I would instead call if (item == null) throw SomeException(); else list.add(item);
. Is there an existing List
class (maybe in Apache Commons or something) that does this for me?
Similar question: Helper to remove null references in a Java List? but I don't want to remove all the nulls, I want to make sure they never get added in the first place.