{"record":{"id":"bd5a9faeb15ac6a0","repo":"TheAlgorithms/Java","slug":"acoeffs-must-be-of-size-got","errorCode":null,"errorMessage":"aCoeffs must be of size {}, got {}","messagePattern":"aCoeffs must be of size (.+?), got (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/audiofilters/IIRFilter.java","lineNumber":50,"sourceCode":"        // Sane defaults\n        coeffsA[0] = 1.0;\n        coeffsB[0] = 1.0;\n\n        historyX = new double[order];\n        historyY = new double[order];\n    }\n\n    /**\n     * Set coefficients\n     *\n     * @param aCoeffs Denominator coefficients\n     * @param bCoeffs Numerator coefficients\n     * @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is\n     * not of size {@code order}, or if {@code aCoeffs[0]} is 0.0\n     */\n    public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {\n        if (aCoeffs.length != order) {\n            throw new IllegalArgumentException(\"aCoeffs must be of size \" + order + \", got \" + aCoeffs.length);\n        }\n\n        if (aCoeffs[0] == 0.0) {\n            throw new IllegalArgumentException(\"aCoeffs.get(0) must not be zero\");\n        }\n\n        if (bCoeffs.length != order) {\n            throw new IllegalArgumentException(\"bCoeffs must be of size \" + order + \", got \" + bCoeffs.length);\n        }\n\n        for (int i = 0; i < order; i++) {\n            coeffsA[i] = aCoeffs[i];\n            coeffsB[i] = bCoeffs[i];\n        }\n    }\n\n    /**\n     * Process a single sample","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java#L32-L68","documentation":"Thrown by IIRFilter.setCoeffs when the denominator coefficient array's length does not equal the filter's configured order. The internal coefficient buffers are sized relative to order, so a mismatched array would either leave coefficients unset or cause an out-of-bounds write during the copy loop. Note the array is compared against `order`, not `order + 1` even though the buffers are allocated as `order + 1`.","triggerScenarios":"Calling `setCoeffs(a, b)` where `a.length != filterOrder`. For example, constructing `new IIRFilter(4)` then calling `setCoeffs(new double[5], ...)` throws with 'aCoeffs must be of size 4, got 5'.","commonSituations":"Loading coefficients from a DSP design tool (e.g. scipy.signal) that returns arrays of length order+1 including the leading a0; mismatch between the order used to construct the filter and the order assumed when generating coefficients; off-by-one when copying a coefficient list.","solutions":["Ensure aCoeffs has exactly `order` elements, matching the length the constructor expects.","If your design tool returns order+1 coefficients (including a0), slice the array to the expected size before passing.","Verify the same order value was used at construction time and at coefficient-generation time."],"exampleFix":"// before\nIIRFilter f = new IIRFilter(4);\nf.setCoeffs(new double[5], b);   // throws: must be of size 4, got 5\n\n// after\nIIRFilter f = new IIRFilter(4);\nf.setCoeffs(Arrays.copyOf(a, 4), Arrays.copyOf(b, 4));","handlingStrategy":"validation","validationCode":"if (aCoeffs.length != order) throw new IllegalArgumentException(\"aCoeffs length \" + aCoeffs.length + \" != order \" + order);\nfilter.setCoeffs(aCoeffs, bCoeffs);","typeGuard":"public static boolean aCoeffsMatchOrder(double[] a, int order) {\n    return a != null && a.length == order;\n}","tryCatchPattern":"try {\n    filter.setCoeffs(a, b);\n} catch (IllegalArgumentException e) {\n    // resize coefficients to the expected length and retry\n    filter.setCoeffs(Arrays.copyOf(a, order), Arrays.copyOf(b, order));\n}","preventionTips":["Generate coefficients with the same order used at construction.","Remember setCoeffs expects length == order (not order+1).","Centralize filter design so order and coefficient length stay in sync."],"tags":["audio","filter","argument-validation","illegalargumentexception","off-by-one"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}