Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does List.addAll() throw IllegalStateException?

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?

like image 897
Marcel Bro Avatar asked Jul 22 '15 11:07

Marcel Bro


People also ask

What does list addAll do in Java?

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.

Why addAll is returning boolean?

The addAll() method returns a Boolean value true, if the queue has changed as a result of this call else it returns false.

How do you use addAll collections?

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.

What is the difference between ADD and addAll in list in Java?

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.


2 Answers

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.

like image 143
David P. Caldwell Avatar answered Sep 30 '22 06:09

David P. Caldwell


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.

like image 23
Anonymous Coward Avatar answered Sep 30 '22 07:09

Anonymous Coward