{"record":{"id":"3c71f491f7cd8bc3","repo":"TheAlgorithms/Java","slug":"matrix-must-contain-at-least-one-element","errorCode":null,"errorMessage":"Matrix must contain at least one element.","messagePattern":"Matrix must contain at least one element\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java","lineNumber":26,"sourceCode":" * Median of Matrix (https://medium.com/@vaibhav.yadav8101/median-in-a-row-wise-sorted-matrix-901737f3e116)\n * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)\n */\n\npublic final class MedianOfMatrix {\n    private MedianOfMatrix() {\n    }\n\n    public static int median(Iterable<List<Integer>> matrix) {\n        List<Integer> flattened = new ArrayList<>();\n\n        for (List<Integer> row : matrix) {\n            if (row != null) {\n                flattened.addAll(row);\n            }\n        }\n\n        if (flattened.isEmpty()) {\n            throw new IllegalArgumentException(\"Matrix must contain at least one element.\");\n        }\n\n        Collections.sort(flattened);\n        return flattened.get((flattened.size() - 1) / 2);\n    }\n}\n","sourceCodeStart":8,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/MedianOfMatrix.java#L8-L33","documentation":"Thrown by MedianOfMatrix.median when, after iterating all rows and skipping null rows, the flattened list is empty. The median of zero elements is undefined, so the method refuses rather than throwing IndexOutOfBoundsException on Collections.sort / get. Note: a matrix whose every row is null also lands here, since flattened stays empty.","triggerScenarios":"Call median with an empty Iterable, an Iterable of all-null rows, or a matrix whose non-null rows are all empty lists.","commonSituations":"A sparse matrix representation with all-null rows, filtering that removed every row, JSON deserialization yielding empty inner lists, or an iterator over a query result with no rows.","solutions":["Check that the matrix has at least one non-empty, non-null row before calling.","Guard with a flattened-size check and return a domain 'no median' value (e.g., OptionalInt.empty()).","Filter out null/empty rows upstream so the matrix is always non-trivial."],"exampleFix":"// before\nint med = MedianOfMatrix.median(matrix);\n\n// after\nboolean hasElements = false;\nfor (List<Integer> row : matrix) {\n    if (row != null && !row.isEmpty()) { hasElements = true; break; }\n}\nif (!hasElements) throw new NoSuchElementException(\"matrix has no elements\");\nint med = MedianOfMatrix.median(matrix);","handlingStrategy":"validation","validationCode":"boolean hasElements = false;\nfor (List<Integer> row : matrix) {\n    if (row != null && !row.isEmpty()) { hasElements = true; break; }\n}\nif (!hasElements) throw new NoSuchElementException(\"matrix has no elements\");\nint med = MedianOfMatrix.median(matrix);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Filter out null/empty rows before constructing the matrix.","Return OptionalInt from a wrapper so 'no median' is an explicit, type-safe outcome."],"tags":["matrix","median","empty-check","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}