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;
// ProcessView on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure bCoeffs has exactly `order` elements.
- Pad or truncate the numerator array to match order before passing.
- 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
- Keep a and b arrays the same length, equal to order.
- Generate both from the same design routine.
- Validate lengths together before calling setCoeffs.
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
- aCoeffs must be of size {}, got {}
- Alpha must be between 0 and 1.
- order must be greater than zero
- aCoeffs.get(0) must not be zero
- Invalid input: 0 ≤ k ≤ n is required.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/14f3b305bb180858.
Report an issue: GitHub.