apache/flink · error · NoSuchElementException

No value present

Error message

No value present

What it means

SerializableOptional is a serializable mirror of java.util.Optional. get() on the empty instance (created via SerializableOptional.empty(), which stores a null value) throws NoSuchElementException, exactly like Optional.get(). isPresent() exists to check first; the error means the code assumed a value that was never present.

Source

Thrown at flink-core/src/main/java/org/apache/flink/types/SerializableOptional.java:44

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

/** Serializable {@link Optional}. */
public final class SerializableOptional<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = -3312769593551775940L;

    private static final SerializableOptional<?> EMPTY = new SerializableOptional<>(null);

    @Nullable private final T value;

    private SerializableOptional(@Nullable T value) {
        this.value = value;
    }

    public T get() {
        if (value == null) {
            throw new NoSuchElementException("No value present");
        }
        return value;
    }

    public boolean isPresent() {
        return value != null;
    }

    public void ifPresent(Consumer<? super T> consumer) {
        if (value != null) {
            consumer.accept(value);
        }
    }

    public <R extends Serializable> SerializableOptional<R> map(
            Function<? super T, ? extends R> mapper) {
        if (value == null) {
            return empty();

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Guard with isPresent() before get(), or use orElse/orElseGet for defaults
  2. Fix the upstream contract if absence is unexpected: make the producer always supply a value
  3. In tests, cover the empty path explicitly so absence stops being an afterthought

Example fix

// before
T v = opt.get(); // NoSuchElementException when empty

// after
T v = opt.isPresent() ? opt.get() : defaultValue;
// or: T v = opt.orElse(defaultValue);
Defensive patterns

Strategy: validation

Validate before calling

if (!opt.isPresent()) {
    return defaultValue; // or throw a domain-specific exception
}
T v = opt.get();

Prevention

When it happens

Trigger: Calling get() without a preceding isPresent() check on an optional returned empty — e.g. from empty(), fromNullable(null), or a deserialized optional whose source had no value.

Common situations: Function inputs that are legitimately absent (optional config/field) reaching code that unconditionally calls get(); deserializing across versions where a field became nullable; porting code from Optional where get() misuse is a known anti-pattern.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/05a22485ff1eb8bd. Report an issue: GitHub.