{"record":{"id":"e0cd2f6d015a8353","repo":"frohoff/ysoserial","slug":"mismatched-columns","errorCode":null,"errorMessage":"mismatched columns","messagePattern":"mismatched columns","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/ysoserial/Strings.java","lineNumber":33,"sourceCode":"            if (! first) sb.append(sep);\n            if (prefix != null) sb.append(prefix);\n            sb.append(s);\n            if (suffix != null) sb.append(suffix);\n            first = false;\n        }\n        return sb.toString();\n    }\n\n    public static String repeat(String str, int num) {\n        final String[] strs = new String[num];\n        Arrays.fill(strs, str);\n        return join(Arrays.asList(strs), \"\", \"\", \"\");\n    }\n\n    public static List<String> formatTable(List<String[]> rows) {\n        final Integer[] maxLengths = new Integer[rows.get(0).length];\n        for (String[] row : rows) {\n            if (maxLengths.length != row.length) throw new IllegalStateException(\"mismatched columns\");\n            for (int i = 0; i < maxLengths.length; i++) {\n                if (maxLengths[i] == null || maxLengths[i] < row[i].length()) {\n                    maxLengths[i] = row[i].length();\n                }\n            }\n        }\n\n        final List<String> lines = new LinkedList<String>();\n        for (String[] row : rows) {\n            for (int i = 0; i < maxLengths.length; i++) {\n                final String pad = repeat(\" \", maxLengths[i] - row[i].length());\n                row[i] = row[i] + pad;\n            }\n            lines.add(join(Arrays.asList(row), \" \", \"\", \"\"));\n        }\n        return lines;\n    }\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/frohoff/ysoserial/blob/218bcffcaaa904a4e392f0c15d9e2874533635a3/src/main/java/ysoserial/Strings.java#L15-L51","documentation":"Strings.formatTable builds a column-width table by iterating rows and asserting every row has the same number of columns as the first row. When a row's length differs from rows.get(0).length, it throws IllegalStateException(\"mismatched columns\") because a rectangular table cannot be formatted from ragged input.","triggerScenarios":"Calling ysoserial.Strings.formatTable(List<String[]>) with a list whose String[] rows have inconsistent lengths — e.g. the first row has 3 elements but a later row has 2 or 4. Note an empty list throws IndexOutOfBoundsException at rows.get(0) instead.","commonSituations":"Building CLI/help output tables where rows are assembled from variable-length data (e.g. payload name + optional description columns), or a refactor adds a field to some row-construction sites but not others.","solutions":["Audit every row construction site so each String[] has the same number of elements as the first row","Normalize rows before the call: pad short rows with empty strings or truncate/merge long rows","Add a unit test asserting all rows share the same length before calling formatTable"],"exampleFix":"// before\nList<String[]> rows = new ArrayList<>();\nrows.add(new String[]{\"name\", \"target\"});\nrows.add(new String[]{\"CommonsCollections1\"}); // 1 column\nStrings.formatTable(rows);\n// after\nrows.add(new String[]{\"CommonsCollections1\", \"\"}); // same column count\nStrings.formatTable(rows);","handlingStrategy":"validation","validationCode":"if (rows.isEmpty()) throw new IllegalArgumentException(\"no rows\");\nint cols = rows.get(0).length;\nfor (String[] row : rows) {\n    if (row.length != cols) throw new IllegalArgumentException(\"row has \" + row.length + \" cols, expected \" + cols);\n}","typeGuard":"static boolean isRectangular(List<String[]> rows) {\n    return rows != null && !rows.isEmpty() &&\n        rows.stream().allMatch(r -> r.length == rows.get(0).length);\n}","tryCatchPattern":"try {\n    Strings.formatTable(rows);\n} catch (IllegalStateException e) {\n    // log offending row lengths and fall back to plain printing\n}","preventionTips":["Build rows with a shared helper/factory so column count cannot drift","Add a unit test asserting all rows have equal length","Prefer a small row record/class over raw String[] where possible"],"tags":["java","string-formatting","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"218bcffcaaa904a4e392f0c15d9e2874533635a3","analyzedAt":"2026-09-12T01:53:58.488Z","contentChangedAt":"2026-09-12T01:53:58.488Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}