{"record":{"id":"7ac0bbbdf905b777","repo":"TheAlgorithms/Java","slug":"matrix-is-empty","errorCode":null,"errorMessage":"Matrix is empty","messagePattern":"Matrix is empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/MatrixTranspose.java","lineNumber":33,"sourceCode":" * @author Rajat-Jain29\n * @version 11.0.9\n * @since 2014-03-31\n */\npublic final class MatrixTranspose {\n    private MatrixTranspose() {\n    }\n\n    /**\n     * Calculate the transpose of the given matrix.\n     *\n     * @param matrix The matrix to be transposed\n     * @throws IllegalArgumentException if the matrix is empty\n     * @throws NullPointerException     if the matrix is null\n     * @return The transposed matrix\n     */\n    public static int[][] transpose(int[][] matrix) {\n        if (matrix == null || matrix.length == 0) {\n            throw new IllegalArgumentException(\"Matrix is empty\");\n        }\n\n        int rows = matrix.length;\n        int cols = matrix[0].length;\n        int[][] transposedMatrix = new int[cols][rows];\n        for (int i = 0; i < cols; i++) {\n            for (int j = 0; j < rows; j++) {\n                transposedMatrix[i][j] = matrix[j][i];\n            }\n        }\n        return transposedMatrix;\n    }\n}\n","sourceCodeStart":15,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/MatrixTranspose.java#L15-L47","documentation":"Thrown by MatrixTranspose.transpose(int[][]) when matrix is null OR matrix.length == 0. Note the Javadoc claims NullPointerException for null and IllegalArgumentException for empty, but the implementation routes both cases to this single IllegalArgumentException ('Matrix is empty'), so null input does NOT produce the documented NPE.","triggerScenarios":"Call transpose(null), transpose(new int[0][]), or pass a matrix reference that was never populated. Also reachable if a grouping operation yielded zero rows.","commonSituations":"Empty result sets materialized as 2D arrays, an uninitialized field, or a parser returning an empty array for a blank input.","solutions":["Null-and-size-check before calling and handle the empty case explicitly.","If you rely on the documented NPE-vs-IAE split, note the implementation differs; do not branch on exception type for null.","Use Optional or an empty-matrix sentinel instead of null."],"exampleFix":"// before\nint[][] t = MatrixTranspose.transpose(m);\n\n// after\nif (m == null || m.length == 0) {\n    return new int[0][0];\n}\nint[][] t = MatrixTranspose.transpose(m);","handlingStrategy":"validation","validationCode":"if (matrix == null || matrix.length == 0) {\n    return new int[0][0]; // or throw a domain exception\n}\nint[][] t = MatrixTranspose.transpose(matrix);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not branch on NullPointerException vs IllegalArgumentException for null input here; the impl throws IAE for both despite the Javadoc.","Avoid null matrices entirely; use empty arrays or Optional as 'no matrix'."],"tags":["matrix","transpose","empty-check","argument-validation","doc-mismatch"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}