When does List.addAll () throw an IllegalStateException?

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?

+8
java list add illegalargumentexception
source share
4 answers

Depending on how much information the user has, this may be unrequited. But I will make an assumption and delete my answer if it turns out that this contradicts this. :)

Assuming you wrote all the code, I agree that addAll() cannot throw an IllegalStateException (and all talk about IllegalArgumentException doesn't matter).

I assume that the error does not arise from the addAll() call, but from another call in the code (not shown) that is trying to manipulate one of these collections. You can get an IllegalStateException by trying, for example, to iterate through a list (using the iterator obtained with .iterator() ) and delete the item, and then try to delete another item without calling Iterator.next() . Similarly, Iterator.set() can drop one in an iterator derived from an ArrayList . Therefore, I assume that somewhere in the manipulation of the list one of these things happens.

Alternatively, there are many ways that other collection implementations can be implemented. Therefore, if we are not sure if this applies to ArrayList , then we have very few options to continue.

+1
source share

Your code can never raise an IllegalArgumentException because ArrayList # addAll cannot throw such an exception.

To get this exception, you must use a class that implements Collection, in which such an exception can be thrown. You can easily make your own by extending the ArrayList and overriding the methods involved.

+1
source share

I don't know any implementations (in jre or any library) that throw an IllegalArgumentException.

But you can easily create your own implementation that throws such an exception. The Collection interface allows users to understand that the constructor can use an IllegalArgumentException - it does not say that implementers really use it.

Example:

 import java.util.ArrayList; public class PickyList<T> extends ArrayList<T> { public static class SpecialThing { } @Override public boolean add(T e) { if (e instanceof SpecialThing) return super.add(e); throw new IllegalArgumentException(); } } 

As a result, I would recommend this approach:

  • test your code by adding semantically sensible elements to your collection. Do not add Person objects to a list named postalCodes
  • handle any technical exception in the mode accordingly . For example, rollback transactions, logging, informing the user and / or support teams.
  • consider informing callers of your method (via JavaDoc) that your method may fail
0
source share

It depends on the implementation. If you check the documentation for ArrayList , which is the actual class you are using, you will see this.

Throws: NullPointerException - if the specified collection is null

Therefore, your ArrayList should not throw an IllegalArgumentException . If you would use a different implementation or shell (see the unmodifiable list , note , so that this example does not raise such an exception), this may cause such an exception.

Get registration from your user and try to reproduce the problem.

0
source share

All Articles