Blog |

How to Fix the Unsupported Operation Exception in Java

How to Fix the Unsupported Operation Exception in Java
Table of Contents
Try Rollbar Today

Proactively discover and resolve errors

An UnsupportedOperationException is a runtime exception in Java that occurs when a requested operation is not supported. For example, if an unmodifiable List is attempted to be modified by adding or removing elements, an UnsupportedOperationException is thrown. This is one of the common exceptions that occur when working with Java collections such as List, Queue, Set and Map.

The UnsupportedOperationException is a member of the Java Collections Framework. Since it is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor.

 

What Causes UnsupportedOperationException

An UnsupportedOperationException is thrown when a requested operation cannot be performed because it is not supported for that particular class. One of the most common causes for this exception is using the asList() method of the java.util.Arrays class. Since this method returns a fixed-size unmodifiable List, the add() or remove() methods are unsupported. Trying to add or remove elements from such a List will throw the UnsupportedOperationException exception.

Other cases where this exception can occur include:

  • Using wrappers between collections and primitive types.
  • Trying to remove elements using an Iterator.
  • Trying to add, remove or set elements using ListIterator.

 

UnsupportedOperationException Example

Here’s an example of an UnsupportedOperationException thrown when an object is attempted to be added to an unmodifiable List:

import java.util.Arrays;
import java.util.List;

public class UnsupportedOperationExceptionExample {
    public static void main(String[] args) {
        String array[] = {"a", "b", "c"};
        List<String> list = Arrays.asList(array);
        list.add("d");
    }
}

Since the Arrays.asList() method returns a fixed-size list, attempting to modify it either by adding or removing elements throws an UnsupportedOperationException.

Running the above code throws the exception:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.add(AbstractList.java:153)
    at java.base/java.util.AbstractList.add(AbstractList.java:111)
    at UnsupportedOperationExceptionExample.main(UnsupportedOperationExceptionExample.java:8)

 

How to Resolve UnsupportedOperationException

The UnsupportedOperationException can be resolved by using a mutable collection, such as ArrayList, which can be modified. An unmodifiable collection or data structure should not be attempted to be modified.

The unmodifiable List returned by the Arrays.asList() method in the earlier example can be passed to a new ArrayList object, which can be modified:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UnsupportedOperationExceptionExample {
    public static void main(String[] args) {
        String array[] = {"a", "b", "c"};
        List<String> list = Arrays.asList(array);

        List<String> arraylist = new ArrayList<>(list);

        arraylist.add("d");

        System.out.println(arraylist);
    }
}

Here, a new ArrayList object is created using the unmodifiable list returned from the Arrays.asList() method. When a new element is added to the ArrayList, it works as expected and resolves the UnsupportedOperationException. Running the above code produces the following output:

[a, b, c, d]

 

 

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Posts

See all posts

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Start continuously improving your code today.

Get Started Shape