ReactiveX/RxJava · error · IllegalArgumentException
Array of size 4 expected but got {}
Error message
Array of size 4 expected but got {} What it means
Thrown by Array4Func.apply(Object[]) when the array length is not exactly 4. Array4Func adapts a Function4 (4-ary) to Function<Object[],R>, backing 4-way zip/combineLatest fan-in. The array arity must equal the number of combined sources.
Source
Thrown at src/main/java/io/reactivex/rxjava4/internal/functions/Functions.java:493
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> {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Throwable {
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);
}View on GitHub (pinned to a8ab535614)
Solutions
- Pass exactly 4 elements in the array to a 4-arity adapter; correct the array-building code.
- Prefer the public 4-source zip/combineLatest APIs which guarantee correct arity.
- Add a unit test verifying length-4 input at the adapter call site.
- Keep array length coupled to the source list size to avoid drift.
Example fix
// before
Function<Object[], R> f = Functions.array4Func(fn4);
R r = f.apply(new Object[]{ a, b, c }); // length 3 -> throws
// after
Function<Object[], R> f = Functions.array4Func(fn4);
R r = f.apply(new Object[]{ a, b, c, d }); // length 4 matches arity Defensive patterns
Strategy: validation
Validate before calling
assert args.length == 4 : "4-arity adapter requires length-4 array";
Try / catch
try {
R r = array4Fn.apply(args);
} catch (IllegalArgumentException e) {
// fix array length to 4
} Prevention
- Prefer public 4-source zip/combineLatest APIs.
- Bind array length to source count; avoid hardcoded literals.
- Add a length-4 input test.
When it happens
Trigger: A 4-arity adapter receives an Object[] whose length != 4, almost always an internal/custom-operator wiring mistake rather than normal application usage.
Common situations: Manually constructing a 4-source fan-in with an array built from a list of indeterminate size; mismatching the adapter arity with the number of sources after refactoring; reflective/dynamic source composition.
Related errors
- Array of size 2 expected but got {}
- Array of size 3 expected but got {}
- Array of size 5 expected but got {}
- Array of size 6 expected but got {}
- Array of size 7 expected but got {}
AI-assisted analysis of ReactiveX/RxJava@a8ab535614 (2026-08-13).
Data as JSON: /api/errors/6147f86457feca0d.
Report an issue: GitHub.