ReactiveX/RxJava · error · IllegalArgumentException

Array of size 5 expected but got {}

Error message

Array of size 5 expected but got {}

What it means

Thrown by Array5Func.apply(Object[]) when the array length is not exactly 5. Array5Func is a class (not a record) adapting Function5 to Function<Object[],R>, backing 5-way zip/combineLatest. The array arity must equal 5.

Source

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

                if (a.length != 4) {
                    throw new IllegalArgumentException("Array of size 4 expected but got " + a.length);
                }
                return f.apply((T1) a[0], (T2) a[1], (T3) a[2], (T4) a[3]);
            }
        }

    static final class Array5Func<T1, T2, T3, T4, T5, R> implements Function<Object[], R> {
        private final Function5<T1, T2, T3, T4, T5, R> f;

        Array5Func(Function5<T1, T2, T3, T4, T5, R> f) {
            this.f = f;
        }

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

    record Array6Func<T1, T2, T3, T4, T5, T6, R>(
            Function6<T1, T2, T3, T4, T5, T6, R> f) implements Function<Object[], R> {

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

View on GitHub (pinned to a8ab535614)

Solutions

  1. Supply exactly 5 array elements to the 5-arity adapter; fix the array-construction logic.
  2. Use the public 5-source zip/combineLatest overloads that handle arity internally.
  3. Write a test asserting length-5 input at the call site.
  4. Bind array length to the actual source count rather than a literal.

Example fix

// before
Function<Object[], R> f = new Functions.Array5Func<>(fn5);
R r = f.apply(new Object[]{ a, b, c, d }); // length 4 -> throws

// after
Function<Object[], R> f = new Functions.Array5Func<>(fn5);
R r = f.apply(new Object[]{ a, b, c, d, e }); // length 5 matches arity
Defensive patterns

Strategy: validation

Validate before calling

assert args.length == 5 : "5-arity adapter requires length-5 array";

Try / catch

try {
    R r = array5Fn.apply(args);
} catch (IllegalArgumentException e) {
    // array length != 5; realign
}

Prevention

When it happens

Trigger: A 5-arity adapter receives an Object[] of length != 5. Typically an internal or custom-operator wiring error; not normally reachable from public API usage.

Common situations: Dynamic source lists feeding a fixed 5-arity adapter; refactoring a fan-in that added/removed a source but left the adapter arity unchanged; manual test scaffolding with the wrong array size.

Related errors


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