TheAlgorithms/Java · error · IllegalArgumentException
Input matrices cannot be null
Error message
Input matrices cannot be null
What it means
Thrown by MatrixMultiplication.multiply when matrixA or matrixB is null. The null check runs first, before any indexing, so a null operand produces a clear message rather than a NullPointerException on matrixA.length / matrixB.length.
Source
Thrown at src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java:39
*
*/
public final class MatrixMultiplication {
private MatrixMultiplication() {
}
/**
* Multiplies two matrices.
*
* @param matrixA the first matrix rowsA x colsA
* @param matrixB the second matrix rowsB x colsB
* @return the product of the two matrices rowsA x colsB
* @throws IllegalArgumentException if the matrices cannot be multiplied
*/
public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
// Check the input matrices are not null
if (matrixA == null || matrixB == null) {
throw new IllegalArgumentException("Input matrices cannot be null");
}
// Check for empty matrices
if (matrixA.length == 0 || matrixB.length == 0 || matrixA[0].length == 0 || matrixB[0].length == 0) {
throw new IllegalArgumentException("Input matrices must not be empty");
}
// Validate the matrix dimensions
if (matrixA[0].length != matrixB.length) {
throw new IllegalArgumentException("Matrices cannot be multiplied: incompatible dimensions.");
}
int rowsA = matrixA.length;
int colsA = matrixA[0].length;
int colsB = matrixB[0].length;
// Initialize the result matrix with zeros
double[][] result = new double[rowsA][colsB];View on GitHub (pinned to fdfb9a395b)
Solutions
- Null-check both operands at the call site and fail or default to a valid matrix.
- Use Objects.requireNonNull(matrixA, "matrixA") to fail earlier with context.
- Ensure deserializers return empty arrays instead of null for absent fields.
Example fix
// before double[][] r = MatrixMultiplication.multiply(a, b); // after Objects.requireNonNull(a, "matrixA"); Objects.requireNonNull(b, "matrixB"); double[][] r = MatrixMultiplication.multiply(a, b);
Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(matrixA, "matrixA"); Objects.requireNonNull(matrixB, "matrixB"); double[][] r = MatrixMultiplication.multiply(matrixA, matrixB);
Prevention
- Use Objects.requireNonNull at the boundary so the failure points at the right argument.
- Configure deserializers to emit empty arrays rather than null for absent matrix fields.
When it happens
Trigger: Call multiply(null, b), multiply(a, null), or pass a matrix reference that was never assigned (e.g., a field left null after a failed initialization).
Common situations: An optional matrix parameter that arrived empty, a deserialization that produced null for an absent JSON field, or a code path that constructs the second operand conditionally and skipped it.
Related errors
- Maze must not be null or empty.
- Input matrices must not be empty
- Matrices cannot be multiplied: incompatible dimensions.
- The input matrix cannot be null
- The input matrix cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/97754e3ef597b539.
Report an issue: GitHub.