{"record":{"id":"6c8475fddf33f41a","repo":"ReactiveX/RxJava","slug":"array-of-size-2-expected-but-got","errorCode":null,"errorMessage":"Array of size 2 expected but got {}","messagePattern":"Array of size 2 expected but got (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/reactivex/rxjava4/internal/functions/Functions.java","lineNumber":469,"sourceCode":"            public List<T> apply(List<T> v) {\n                v.sort(comparator);\n                return v;\n            }\n        }\n\n    public static <T> Function<List<T>, List<T>> listSorter(final Comparator<? super T> comparator) {\n        return new ListSorter<>(comparator);\n    }\n\n    public static final Consumer<Subscription> REQUEST_MAX = new MaxRequestSubscription();\n\n    record Array2Func<T1, T2, R>(BiFunction<? super T1, ? super T2, ? extends R> f) implements Function<Object[], R> {\n\n        @SuppressWarnings(\"unchecked\")\n            @Override\n            public R apply(Object[] a) throws Throwable {\n                if (a.length != 2) {\n                    throw new IllegalArgumentException(\"Array of size 2 expected but got \" + a.length);\n                }\n                return f.apply((T1) a[0], (T2) a[1]);\n            }\n        }\n\n    record Array3Func<T1, T2, T3, R>(Function3<T1, T2, T3, R> f) implements Function<Object[], R> {\n\n        @SuppressWarnings(\"unchecked\")\n            @Override\n            public R apply(Object[] a) throws Throwable {\n                if (a.length != 3) {\n                    throw new IllegalArgumentException(\"Array of size 3 expected but got \" + a.length);\n                }\n                return f.apply((T1) a[0], (T2) a[1], (T3) a[2]);\n            }\n        }\n\n    record Array4Func<T1, T2, T3, T4, R>(Function4<T1, T2, T3, T4, R> f) implements Function<Object[], R> {","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/ReactiveX/RxJava/blob/a8ab5356143f37a8f1dd6d76c79191bec5fa343b/src/main/java/io/reactivex/rxjava4/internal/functions/Functions.java#L451-L487","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the Object[] passed to a 2-arity adapter always has exactly 2 elements — fix the operator/utility that builds the array.","Prefer using the public zip/combineLatest APIs (which construct the correct arity internally) over hand-rolling array adapters.","Add an assertion/test verifying the array length equals the function arity at the construction site.","If authoring a custom fan-in, derive the array length from the number of sources, not from a hardcoded constant."],"exampleFix":"// before\nFunction<Object[], R> f = Functions.array2Func(biFn);\nR r = f.apply(new Object[]{ only }); // length 1 -> throws\n\n// after\nFunction<Object[], R> f = Functions.array2Func(biFn);\nR r = f.apply(new Object[]{ a, b }); // length 2 matches arity","handlingStrategy":"validation","validationCode":"assert args.length == 2 : \"2-arity adapter requires length-2 array\";\nBiFunction<T1,T2,R> biFn = ...;\n// ensure the operator wires exactly 2 sources to this adapter","typeGuard":null,"tryCatchPattern":"try {\n    R r = array2Fn.apply(args);\n} catch (IllegalArgumentException e) {\n    // arity mismatch; recheck source count vs adapter arity\n}","preventionTips":["Prefer public zip/combineLatest APIs over manual array-arity adapters.","Drive the Object[] length from the actual source count, never a separate literal.","Add arity assertions in custom fan-in operator tests."],"tags":["validation","argument","functions","arity","zip","rxjava"],"backgroundTag":null,"analyzedSha":"a8ab5356143f37a8f1dd6d76c79191bec5fa343b","analyzedAt":"2026-08-13T23:25:30.069Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}