TheAlgorithms/Java · error · IllegalArgumentException
Matrices cannot be multiplied: incompatible dimensions.
Error message
Matrices cannot be multiplied: incompatible dimensions.
What it means
Thrown by MatrixMultiplication.multiply when matrixA's column count (matrixA[0].length) does not equal matrixB's row count (matrixB.length). Matrix multiplication A x B is only defined when cols(A) == rows(B); otherwise the inner k-loop would index out of bounds, so the guard rejects the operation with a clear message.
Source
Thrown at src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java:49
* @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];
// Perform matrix multiplication
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;View on GitHub (pinned to fdfb9a395b)
Solutions
- Verify matrixA[0].length == matrixB.length before calling; transpose one operand if needed.
- Log both shapes at the call site to diagnose mismatches quickly.
- Wrap multiplication in a helper that documents the cols(A)==rows(B) contract.
Example fix
// before
double[][] r = MatrixMultiplication.multiply(a, b);
// after
if (a[0].length != b.length) {
// transpose b or swap operands as appropriate
b = MatrixTranspose.doubleTranspose(b); // your transpose helper
}
double[][] r = MatrixMultiplication.multiply(a, b); Defensive patterns
Strategy: validation
Validate before calling
if (matrixA[0].length != matrixB.length) {
throw new IllegalArgumentException(
"cols(A)=" + matrixA[0].length + " must equal rows(B)=" + matrixB.length);
}
double[][] r = MatrixMultiplication.multiply(matrixA, matrixB); Prevention
- Log both shapes at the call site to diagnose transposition/order mistakes fast.
- Keep a single helper for 'can multiply' checks and reuse it before every multiply call.
When it happens
Trigger: Call multiply on a 2x3 matrix A and a 2x4 matrix B (cols(A)=3 != rows(B)=2). Common with transposed operands, swapped argument order, or data loaded with rows/columns inverted.
Common situations: Transposing one matrix but not the other, passing (B, A) instead of (A, B), CSV loaded as row-major when the algorithm expects column-major, or a refactor that changed orientation.
Related errors
- Input matrices cannot be null
- Input matrices must not be empty
- Maze must not be null or empty.
- Maze must be a square (n x n) matrix.
- Matrix A cannot be empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/2ee6ba934580aa84.
Report an issue: GitHub.