简体   繁体   中英

java.lang.UnsupportedOperationException but no Arrays.asList()

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)

I guess you have at least one of these problems:

  1. Import the wrong 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).
  2. You are running an older version of your code: The line marked by your command ( return unmappedParams; ) would never throw a UnsupportedOperationException .
  3. You are using a strange JRE version. If the problem is not solved by the first two suggestions, please add the exact version of your JRE (by running 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.

I think the root for this kind of Exception could be that the List is Immutable

See the java doc for this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM