All Products
Search
Document Center

Mobile Testing - Deprecated:java_lang_UnsupportedOperationException

Last Updated:Feb 24, 2025

Problem description

This exception occurs when a requested operation is not supported. This exception includes the following categories:

  1. HeadlessException occurs when you invoke the code related to keyboards, monitors, or mouses in an environment where keyboards, monitors, or mouses are not supported.

  2. ReadOnlyBufferException occurs when you invoke a content-mutation method (such as the put or compact method) on the read-only buffer.

  3. ReadOnlyFileSystemExceptiono occurs when you update an object of a file on which read-only permissions are granted.

Solution

This exception occurs when a requested operation is not supported. Check the code based on the stack information and check whether the operations are supported based on official documentation and state diagrams.

Example 1

  java.lang.UnsupportedOperationException
      java.util.AbstractList.add(UnknownSource)
      java.util.AbstractList.add(UnknownSource)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Problem description: Official documentation shows that you can change the structure of a list object returned by the asList() function. That means the remove and add operations are not supported.

Solution: You can create a list object and add the list that meets the requirements to the new object.

Sample code

  public void chooseObject(String[] array, String key){
        List<String> list=Arrays.asList(array);
        /* Error code
        list.remove(0); */
        List<String> newList=new ArrayList<String>();
        for(String tmp:list){
          if(tmp.contains(key)){
            newList.add(tmp);
          }
        }
        System.out.println(newList.toString());
  }

References