ReactiveX/RxJava · error · IllegalArgumentException

Array of size 2 expected but got {}

Error message

Array of size 2 expected but got {}

What it means

Thrown by the Array2Func.apply(Object[]) adapter when the supplied array does not have exactly 2 elements. Array2Func bridges a BiFunction to the Function<Object[],R> signature used internally by zip/combineLatest-style operators that fan-in 2 sources; the array length must match the arity the function was created for. A length mismatch indicates the function was wired to the wrong number of sources.

Source

Thrown at src/main/java/io/reactivex/rxjava4/internal/functions/Functions.java:469

            public List<T> apply(List<T> v) {
                v.sort(comparator);
                return v;
            }
        }

    public static <T> Function<List<T>, List<T>> listSorter(final Comparator<? super T> comparator) {
        return new ListSorter<>(comparator);
    }

    public static final Consumer<Subscription> REQUEST_MAX = new MaxRequestSubscription();

    record Array2Func<T1, T2, R>(BiFunction<? super T1, ? super T2, ? extends R> f) implements Function<Object[], R> {

        @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) throws Throwable {
                if (a.length != 2) {
                    throw new IllegalArgumentException("Array of size 2 expected but got " + a.length);
                }
                return f.apply((T1) a[0], (T2) a[1]);
            }
        }

    record Array3Func<T1, T2, T3, R>(Function3<T1, T2, T3, R> f) implements Function<Object[], R> {

        @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) throws Throwable {
                if (a.length != 3) {
                    throw new IllegalArgumentException("Array of size 3 expected but got " + a.length);
                }
                return f.apply((T1) a[0], (T2) a[1], (T3) a[2]);
            }
        }

    record Array4Func<T1, T2, T3, T4, R>(Function4<T1, T2, T3, T4, R> f) implements Function<Object[], R> {

View on GitHub (pinned to a8ab535614)

Solutions

  1. Ensure the Object[] passed to a 2-arity adapter always has exactly 2 elements — fix the operator/utility that builds the array.
  2. Prefer using the public zip/combineLatest APIs (which construct the correct arity internally) over hand-rolling array adapters.
  3. Add an assertion/test verifying the array length equals the function arity at the construction site.
  4. If authoring a custom fan-in, derive the array length from the number of sources, not from a hardcoded constant.

Example fix

// before
Function<Object[], R> f = Functions.array2Func(biFn);
R r = f.apply(new Object[]{ only }); // length 1 -> throws

// after
Function<Object[], R> f = Functions.array2Func(biFn);
R r = f.apply(new Object[]{ a, b }); // length 2 matches arity
Defensive patterns

Strategy: validation

Validate before calling

assert args.length == 2 : "2-arity adapter requires length-2 array";
BiFunction<T1,T2,R> biFn = ...;
// ensure the operator wires exactly 2 sources to this adapter

Try / catch

try {
    R r = array2Fn.apply(args);
} catch (IllegalArgumentException e) {
    // arity mismatch; recheck source count vs adapter arity
}

Prevention

When it happens

Trigger: An Array2Func (created for 2 sources) receives an Object[] of length other than 2. This is an internal wiring error in a 2-way zip/combine operator; it is not normally reachable from application code unless you build custom fan-in operators using Functions.arrayFunction helpers.

Common situations: Custom operators or testing utilities that manually construct array-arity function adapters and mismatch the arity; reflective or generic code that assembles the Object[] payload dynamically with the wrong count; bugs in operator implementations that pass through the wrong source count.

Related errors


AI-assisted analysis of ReactiveX/RxJava@a8ab535614 (2026-08-13). Data as JSON: /api/errors/6c8475fddf33f41a. Report an issue: GitHub.