I have 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 can it happen for IllegalStateException
to be thrown. It apparently happened to a user of my app, but I'm not able to track what was wrong.
The documentation of Collection.addAll()
says:
IllegalArgumentException - if not all the elements can be added at this time due to insertion restrictions
But what are the insertion restrictions?
I guess it depends on the exact type of the collection. I'm using ArrayList, so let's check docs for addAll()
of List
interface:
IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list
Well, what element property could prevent the element to be added to the List? My both Collections are of the same type, I should be able to add null values..
Can anybody explain this to me, please?
List addAll() Method in Java with Examples. This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
The addAll() method returns a Boolean value true, if the queue has changed as a result of this call else it returns false.
The addAll() method of java. util. Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.
add is used when you need to add single element into collection. addAll is used when you want to add all elements from source collection to your collection. In this particular case you're using ArrayList without specifying a generic argument, so ArrayList<Object> is assumed.
Depending on how much information you have from the user, this may be unanswerable. But I'll make a guess, and delete my answer if further evidence comes to light that contravenes it. :)
Assuming you wrote all the code, I agree that addAll()
cannot throw IllegalStateException
(and all the talk about IllegalArgumentException
is irrelevant).
My guess is that the error does not originate from the addAll()
call but from another call in the code (not shown) that attempts to manipulate one of these collections. It is possible to get an IllegalStateException
by attempting to, for example, iterate through the list (using an iterator obtained with .iterator()
) and remove an item, then attempt to remove another item without calling Iterator.next()
. Similarly, Iterator.set()
can throw one in an iterator obtained from an ArrayList
. So my guess is, somewhere in manipulating the list, one of these things happens.
Alternatively, there are many ways that other collection implementations could throw one. So if we're not sure it pertained to the ArrayList
, then we've got very little to go on.
Your code can never throw IllegalArgumentException because ArrayList#addAll cannot throw such exception.
In order to get that exception you have to use a class implementing Collection where such exception can be thrown. You could easily make your own by extending ArrayList and overriding involved methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With