I have a code
private List<Field> subFields; private Collection<Field> subFieldsCollection; ... try { if (subFields == null && subFieldsCollection != null && !subFieldsCollection.isEmpty()) { subFields = new ArrayList<>(); subFields.addAll(subFieldsCollection); } } catch (IllegalStateException e) { ... }
and I'm wondering how this could happen for an IllegalStateException . Apparently this happened to the user of my application, but I can’t keep track of what’s wrong.
The documentation for Collection.addAll() says:
IllegalArgumentException - if not all elements can be added at this time due to insertion restrictions
But what are the limitations of the insert?
I think it depends on the exact type of collection. I use ArrayList, so let's check the docs for addAll() the List interface:
IllegalArgumentException - if any property of an element of the specified collection does not allow adding it to this list
Well, what property of an element can prevent it from being added to the list? My both collections are of the same type, I should be able to add null values.
Can someone explain this to me please?
java list add illegalargumentexception
Marcel bro
source share