TheAlgorithms/Java · error · IllegalArgumentException
The input matrix cannot be null
Error message
The input matrix cannot be null
What it means
Thrown by MatrixUtil.validateInputMatrix (the double[][] overload) when the matrix argument is null. MatrixUtil also handles BigDecimal matrices via separate helpers; this validator guards the primitive double matrix path. A null matrix has no dimensions or rows for subsequent hasValidRows/isJaggedMatrix checks, so it is rejected first.
Source
Thrown at src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java:31
private MatrixUtil() {
}
private static boolean isValid(final BigDecimal[][] matrix) {
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
}
private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
return isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length;
}
private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length;
}
public static void validateInputMatrix(double[][] matrix) {
if (matrix == null) {
throw new IllegalArgumentException("The input matrix cannot be null");
}
if (matrix.length == 0) {
throw new IllegalArgumentException("The input matrix cannot be empty");
}
if (!hasValidRows(matrix)) {
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
}
if (isJaggedMatrix(matrix)) {
throw new IllegalArgumentException("The input matrix cannot be jagged");
}
}
private static boolean hasValidRows(double[][] matrix) {
for (double[] row : matrix) {
if (row == null || row.length == 0) {
return false;
}
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the double[][] is instantiated before calling MatrixUtil.
- Fix the upstream null-return path in the data source.
- Add an early null guard at the call site with a context-specific message.
Example fix
// before
double[][] a = readMatrix(file);
MatrixUtil.validateInputMatrix(a); // throws if readMatrix returned null
// after
double[][] a = readMatrix(file);
if (a == null) throw new IllegalStateException("Matrix missing for " + file);
MatrixUtil.validateInputMatrix(a); Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(matrix, "matrix"); MatrixUtil.validateInputMatrix(matrix);
Type guard
static boolean isUsableMatrix(double[][] m) {
return m != null && m.length > 0;
} Prevention
- Make matrix loaders return empty arrays, never null.
- Add Objects.requireNonNull at the trust boundary.
When it happens
Trigger: Calling a MatrixUtil operation with a double[][] that is null — e.g., a result of a method returning null on error, a field never assigned, or a deserialized object missing its matrix field.
Common situations: Upstream data source returns null on missing input, refactoring dropped the assignment, or a null propagated through a chain of operations.
Related errors
- The input matrix cannot be null
- Input matrices cannot be null
- The input matrix cannot be empty
- The input matrix cannot have null or empty rows
- The input matrix cannot be jagged
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f1b3bac5d96d5647.
Report an issue: GitHub.