Chris311
Chris311

Reputation: 3992

Unsupported Operation on AbstractList.add()

I got a method

foo(list);

that get's a

List<SomeEntit>

as input.

My method foo looks somewhat like the following:

public void foo(List<SomeEntity someEntities) {
    someEntities.add(anotherEntity);
}

I then get an "javax.ejb.EJBException: java.lang.UnsupportedOperationException" caused by "java.lang.UnsupportedOperationException: null" at "at java.util.AbstractList.add(AbstractList.java:148)"

Can you tell me why this is happening? I hope that my code example is not too minimal.

Upvotes: 2

Views: 5035

Answers (2)

Claude Martin
Claude Martin

Reputation: 765

Some lists are unmodifiable. The operation of adding elements is then "unsupported".

Java collections framework does not have a distinct type for unmodifiable lists or other unmodifiable collections. You never really know if it is allowed to add something. All you can do is to specify that the list that is passed must be modifiable.

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53839

It seems that the actual type of the List you get as the input does not override the add method.

Try converting that list to a list implementation that does, like ArrayList:

 List<SomeEntity> newList = new ArrayList<>(list);
 foo(newList);

Upvotes: 0

Related Questions