TheAlgorithms/Java · error · IllegalArgumentException

The input matrix cannot be jagged

Error message

The input matrix cannot be jagged

What it means

Thrown by MatrixUtil.validateInputMatrix when isJaggedMatrix detects rows of differing lengths. Matrix operations assume a rectangular matrix; ragged rows break column indexing. The check runs after null, empty, and row-validity checks.

Source

Thrown at src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java:40

        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;
            }
        }
        return true;
    }

    /**
     * @brief Checks if the input matrix is a jagged matrix.
     * Jagged matrix is a matrix where the number of columns in each row is not the same.
     *
     * @param matrix The input matrix
     * @return True if the input matrix is a jagged matrix, false otherwise

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Normalize all rows to a common width before the call.
  2. Make the parser enforce equal-length rows or reject ragged input.
  3. Log each row's length to find the mismatch.

Example fix

// before
double[][] m = { {1,2}, {3,4,5} };
MatrixUtil.validateInputMatrix(m);

// after
int width = Arrays.stream(m).mapToInt(r -> r.length).max().getAsInt();
double[][] rect = new double[m.length][width];
for (int i=0;i<m.length;i++) System.arraycopy(m[i],0,rect[i],0,m[i].length);
MatrixUtil.validateInputMatrix(rect);
Defensive patterns

Strategy: validation

Validate before calling

int w = matrix[0].length;
for (double[] row : matrix) {
    if (row.length != w) throw new IllegalStateException("Jagged matrix");
}

Type guard

static boolean isRectangular(double[][] m) {
    if (m == null || m.length == 0) return false;
    int w = m[0].length;
    for (double[] row : m) if (row.length != w) return false;
    return true;
}

Prevention

When it happens

Trigger: Passing a double[][] whose rows have different lengths (e.g., { {1,2}, {3,4,5} }), a parser that did not normalize line lengths, or manual construction with a mismatched inner array size.

Common situations: Inconsistent input line lengths, refactoring copy errors, or assembling a matrix from heterogeneous sources without uniform width.

Related errors


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