Reputation: 31
Getting the below exception which usually comes when we assign a list from a array using Arrays.asList() but i could not see any usage of Arrays in the code where it is being thrown. Moreover the list is initialized using new ArrayList();
protected List getUnmapParam(PPlan pPlan){
List unmappedParams = super.getUnmapParam(pPlan, ord);
PricePlanExt apPP = null;
if (pricePlan.getapID() != null) {
apPP = getCurrentlyItem(pPlan.getID(), errorCodeH);
}
if (apPP != null) {
List billParams = apPP.getBillParams();
for (BillParam billParam : billParams) {
if (billParam.getnameVal().equals("SD")) {
BillUnmapParamType unmappedParamType = getUnMappedType();
Attribute attr = mapSimpleParameter(unmappedParamType, apPP, billParam);
unmappedParams.add(attr);//Here it is being thrown
}
}
}
return unmappedParams;
}
//Super method
protected List getUnmapParameters(Plan pPlan, Ord ord){
return Collections.EMPTY_LIST;
}
The stacktrace:
java.lang.UnsupportedOperationException at
java.util.AbstractList.add(AbstractList.java:148) at
java.util.AbstractList.add(AbstractList.java:108) at
java.som.impl.oshooks.BillingImpl.getUnmapParam(BillingImpl.java:121)
Upvotes: 0
Views: 1031
Reputation: 2208
Your problem is because of Collections.EMPTY_LIST
In general, when seeing that UnsupportedOperationException is being thrown by add, etc. it's typically an indication that some code is trying to modify a non-resizable or unmodifiable collection.
For example, Collections.EMPTY_LIST or Collections.SINGLETON_LIST (which return unmodifiable collections) may be used as optimizations but accidentally be passed into methods that try to modify them.
See UnsupportedOperationException at java.util.AbstractList.add for a more detailed answer
Upvotes: 2
Reputation: 5996
I guess you have at least one of these problems:
ArrayList
: No add(…)
method of java.util.ArrayList
throws a UnsupportedOperationException
. Looking at your stack trace indicates that the ArrayList
you are using does not even override AbstractList<E>.add(E)
(java.util.ArrayList
does).return unmappedParams;
) would never throw a UnsupportedOperationException
.java -version
).After the questions edit: Now it is clear, the answer about the list being immutable is correct: You try to add something to a list returned by Collections.EMPTY_LIST
. According to the Javadoc this list is immutable.
Upvotes: 2
Reputation: 3561
I think the root for this kind of Exception could be that the List is Immutable
See the java doc for this
Upvotes: 1