TheAlgorithms/Java · error · IllegalArgumentException

bCoeffs must be of size {}, got {}

Error message

bCoeffs must be of size {}, got {}

What it means

Thrown by IIRFilter.setCoeffs when the numerator coefficient array's length does not equal the filter's configured order. Like the aCoeffs check, this prevents a partial copy or out-of-bounds access in the loop that fills coeffsB. The length is compared against `order`, not `order + 1`.

Source

Thrown at src/main/java/com/thealgorithms/audiofilters/IIRFilter.java:58

    /**
     * Set coefficients
     *
     * @param aCoeffs Denominator coefficients
     * @param bCoeffs Numerator coefficients
     * @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is
     * not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
     */
    public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
        if (aCoeffs.length != order) {
            throw new IllegalArgumentException("aCoeffs must be of size " + order + ", got " + aCoeffs.length);
        }

        if (aCoeffs[0] == 0.0) {
            throw new IllegalArgumentException("aCoeffs.get(0) must not be zero");
        }

        if (bCoeffs.length != order) {
            throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length);
        }

        for (int i = 0; i < order; i++) {
            coeffsA[i] = aCoeffs[i];
            coeffsB[i] = bCoeffs[i];
        }
    }

    /**
     * Process a single sample
     *
     * @param sample the sample to process
     * @return the processed sample
     */
    public double process(double sample) {
        double result = 0.0;

        // Process

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure bCoeffs has exactly `order` elements.
  2. Pad or truncate the numerator array to match order before passing.
  3. Confirm both a and b arrays are generated with the same order used at construction.

Example fix

// before
f.setCoeffs(a, new double[3]);  // throws: must be of size 4, got 3

// after
double[] bFixed = Arrays.copyOf(b, 4);
f.setCoeffs(a, bFixed);
Defensive patterns

Strategy: validation

Validate before calling

if (bCoeffs.length != order) throw new IllegalArgumentException("bCoeffs length " + bCoeffs.length + " != order " + order);
filter.setCoeffs(aCoeffs, bCoeffs);

Type guard

public static boolean bCoeffsMatchOrder(double[] b, int order) {
    return b != null && b.length == order;
}

Try / catch

try {
    filter.setCoeffs(a, b);
} catch (IllegalArgumentException e) {
    filter.setCoeffs(a, Arrays.copyOf(b, order));
}

Prevention

When it happens

Trigger: Calling `setCoeffs(a, b)` where `b.length != filterOrder`. For example, `new IIRFilter(4)` then `setCoeffs(a, new double[3])` throws 'bCoeffs must be of size 4, got 3'.

Common situations: Numerator and denominator arrays of different lengths coming from a design tool; off-by-one when assembling the coefficient list; reusing arrays sized for a different filter order.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/14f3b305bb180858. Report an issue: GitHub.