Lambda Expression Target Type Error

error: incompatible types: lambda expression target type must be a functional interface

Quick Answer

A lambda can only be assigned to a functional interface (interface with exactly one abstract method). The target type must be a @FunctionalInterface.

Why This Happens

Lambda expressions in Java require a target type that is a functional interface, which is an interface with exactly one abstract method. You cannot assign a lambda to Object, a class type, or an interface with multiple abstract methods.

The Problem

Object fn = () -> System.out.println("hello"); // Error: Object is not a functional interface

The Fix

Runnable fn = () -> System.out.println("hello"); // Runnable is a functional interface
// Or use a specific functional interface:
Consumer<String> printer = s -> System.out.println(s);

Step-by-Step Fix

  1. 1

    Identify the target type

    Check what type the lambda is being assigned to or what parameter type it is being passed as.

  2. 2

    Verify it is a functional interface

    The target type must be an interface with exactly one abstract method. Check if it has @FunctionalInterface annotation.

  3. 3

    Use the correct functional interface

    Use Runnable, Supplier<T>, Consumer<T>, Function<T,R>, Predicate<T>, or define your own @FunctionalInterface.

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