Why Arrays.asList() Can Crash Your Java App and How to Fix It

This article explains why using Java's Arrays.asList() to convert an array into a List can cause UnsupportedOperationException at runtime, illustrates the hidden fixed‑size list implementation, shows the severe impact on an e‑commerce system, and provides a safe solution using java.util.ArrayList to avoid such crashes.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Arrays.asList() Can Crash Your Java App and How to Fix It

Introduction

In Java development, converting arrays to collections is common. The Arrays.asList() method is often used to turn an array into a List, but it hides a serious pitfall that can cause runtime failures.

Incident Review

The issue appeared while developing an e‑commerce order system. An array of order IDs was converted to a List using Arrays.asList(), and later the code attempted to add a new ID, which threw an UnsupportedOperationException and halted the order processing flow.

Impact Analysis

User experience decline : Orders could not be placed, hurting user satisfaction.

Business interruption : The platform accumulated a backlog of orders.

Economic loss : Potential revenue was lost.

Trust crisis : Repeated failures eroded user trust.

Problem Description

The code snippet that caused the failure:

Integer[] arr = {1, 2};
List<Integer> list = Arrays.asList(arr);
list.add(3); // throws UnsupportedOperationException

The exception occurs because the list returned by Arrays.asList() is a fixed‑size view backed by the original array.

Internal Implementation of Arrays.asList()

Arrays.asList(arr)

returns an internal class ArrayList (not java.util.ArrayList) that extends AbstractList. This internal class does not implement add or remove; the default implementations in AbstractList throw UnsupportedOperationException.

Therefore, attempts to modify the list result in the exception.

Solution

Wrap the fixed‑size list with a mutable java.util.ArrayList before performing add/remove operations:

Integer[] arr = {1, 2};
List<Integer> list = new ArrayList<>(Arrays.asList(arr));
list.add(3); // works
list.remove(1); // works

Full example:

public class ArraysBugDemo {
    public static void main(String[] args) {
        Integer[] arr = {1, 2};
        List<Integer> list = Arrays.asList(arr);
        try {
            list.add(3);
        } catch (UnsupportedOperationException e) {
            System.out.println("list.add(3) error: " + e.getMessage());
        }
        ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(arr));
        arrayList.add(3);
        arrayList.forEach(System.out::println);
    }
}

Conclusion

The Arrays.asList() method returns a fixed‑length list without add and remove support. To modify the collection, wrap it with a regular ArrayList (e.g., new ArrayList<>(Arrays.asList(arr))). This prevents runtime crashes and improves code robustness.

Collectionsarrays.aslistArrayListunsupportedoperationexception
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.