Unchecked Cast Warning

warning: [unchecked] unchecked cast: Object to List<String>

Quick Answer

You are casting a raw or Object type to a parameterized type. This is unsafe because generic type information is erased at runtime.

Why This Happens

Due to type erasure, Java cannot verify generic types at runtime. Casting Object to List<String> compiles with a warning but could fail at runtime with ClassCastException. The compiler warns you because it cannot guarantee type safety.

The Problem

Object obj = getFromCache("key");
List<String> list = (List<String>) obj; // Unchecked cast warning

The Fix

Object obj = getFromCache("key");
if (obj instanceof List<?>) {
    List<?> rawList = (List<?>) obj;
    List<String> list = new ArrayList<>();
    for (Object item : rawList) {
        if (item instanceof String) {
            list.add((String) item);
        }
    }
}

Step-by-Step Fix

  1. 1

    Identify the unchecked cast

    Find the cast that produces the warning. It involves a parameterized type like List<String> or Map<String, Integer>.

  2. 2

    Assess the risk

    Determine if the cast is actually safe based on your program logic, or if it could fail at runtime.

  3. 3

    Use type-safe alternatives

    Use instanceof checks, typed collections, or redesign the API to avoid raw types. Use @SuppressWarnings("unchecked") only as a last resort with a comment explaining why it is safe.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free