Incompatible Types with Generics

error: incompatible types: List<Object> cannot be converted to List<String>

Quick Answer

Java generics are invariant. List<Object> is not a supertype of List<String>. Use wildcards like List<? extends Object> for flexibility.

Why This Happens

Unlike arrays, Java generics are invariant. A List<String> is not a subtype of List<Object> even though String is a subtype of Object. This prevents type safety violations at runtime. Use bounded wildcards (? extends, ? super) for flexibility.

The Problem

List<String> strings = new ArrayList<>();
List<Object> objects = strings; // Compile error: incompatible types

The Fix

List<String> strings = new ArrayList<>();
List<? extends Object> objects = strings; // Wildcard allows this

// Or use the upper bound you need:
public void printAll(List<?> items) {
    for (Object item : items) {
        System.out.println(item);
    }
}

Step-by-Step Fix

  1. 1

    Identify the type mismatch

    Read the compile error to see which generic types are incompatible.

  2. 2

    Understand variance

    Java generics are invariant: List<Sub> is not assignable to List<Super>. Use ? extends for covariance, ? super for contravariance.

  3. 3

    Apply appropriate wildcard

    Use ? extends T to read from a collection of subtypes, ? super T to write to a collection of supertypes.

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