0

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)
3
  • 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) Commented May 15, 2015 at 15:21
  • 3
    add the stacktrace to question not in comments use edit option Commented May 15, 2015 at 15:23
  • 1
    Besides the actual problem: You should not put any of your classes in a package starting with java or javax. See the documentation about naming a package. Commented May 15, 2015 at 15:48

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

java version is 1.7.52 import java.util.ArrayList; import java.util.List;
@Sachin: What about my second guess? To be sure remove all .class files and rebuild your whole project. Then please also update your comment "Here it is being thrown".
I think i got it, i have actually edited the code above. There is a call to super which returns a Collections.EMPTY_LIST and when the overridden method is called, it tries to add to this list causing the UnsupportedException.
@Sachin: Just saw your update. I also added the explanation to my answer. I still don't believe the exception is thrown in the line you indicate though. ;-)
Forgot to correct the exception line. Corrected it :-)
|
2

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

Comments

1

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

See the java doc for this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.