{"record":{"id":"7a046bacbd2af08c","repo":"apache/flink","slug":"the-field-is-invalid","errorCode":null,"errorMessage":"The field  ({} is invalid.","messagePattern":"The field  \\((.+?) is invalid\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java","lineNumber":538,"sourceCode":"            } else {\n                // delimiter found.\n                return i + delim.length;\n            }\n        }\n    }\n\n    @SuppressWarnings(\"unused\")\n    protected static void checkAndCoSort(int[] positions, Class<?>[] types) {\n        if (positions.length != types.length) {\n            throw new IllegalArgumentException(\n                    \"The positions and types must be of the same length\");\n        }\n\n        TreeMap<Integer, Class<?>> map = new TreeMap<Integer, Class<?>>();\n\n        for (int i = 0; i < positions.length; i++) {\n            if (positions[i] < 0) {\n                throw new IllegalArgumentException(\n                        \"The field \" + \" (\" + positions[i] + \") is invalid.\");\n            }\n            if (types[i] == null) {\n                throw new IllegalArgumentException(\"The type \" + i + \" is invalid (null)\");\n            }\n\n            if (map.containsKey(positions[i])) {\n                throw new IllegalArgumentException(\n                        \"The position \" + positions[i] + \" occurs multiple times.\");\n            }\n\n            map.put(positions[i], types[i]);\n        }\n\n        int i = 0;\n        for (Map.Entry<Integer, Class<?>> entry : map.entrySet()) {\n            positions[i] = entry.getKey();\n            types[i] = entry.getValue();","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java#L520-L556","documentation":"Thrown by checkAndCoSort when an entry in the positions array is negative. Positions are CSV column indices and must be >= 0. (Note: the message string has a stray double space and a missing closing parenthesis before the placeholder — it prints as 'The field  ( -1 is invalid.'.) This is a programming-error guard.","triggerScenarios":"A subclass passes a positions array containing a negative value, typically from an off-by-one decrement, an uninitialized int defaulting through logic, or a sentinel -1 not filtered out before the call.","commonSituations":"Index computed as something - 1 when something is 0; mixing 0-based and 1-based indexing; reusing a 'not set' sentinel (-1) without filtering; refactor that left a placeholder index.","solutions":["Validate every position is >= 0 before calling checkAndCoSort.","Audit index arithmetic around the call for off-by-one and 0-vs-1 based indexing mistakes.","Filter out sentinel/placeholder indices before passing them in."],"exampleFix":"// before\nint[] positions = computeIndices(); // may contain -1 sentinel\ncheckAndCoSort(positions, types);\n// after\nint[] positions = Arrays.stream(computeIndices()).filter(i -> i >= 0).toArray();\ncheckAndCoSort(positions, types);","handlingStrategy":"validation","validationCode":"int[] sanitized = Arrays.stream(positions).filter(i -> i >= 0).toArray();\nif (sanitized.length != positions.length) {\n    throw new IllegalArgumentException(\"Negative position found in \" + Arrays.toString(positions));\n}\ncheckAndCoSort(sanitized, types);","typeGuard":"static int nonNegIndex(int i) {\n    if (i < 0) throw new IllegalArgumentException(\"index must be >= 0, got \" + i);\n    return i;\n}","tryCatchPattern":"try {\n    checkAndCoSort(positions, types);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"invalid\")) {\n        throw new IllegalArgumentException(\"A CSV column position is negative: \" + Arrays.toString(positions), e);\n    }\n    throw e;\n}","preventionTips":["Filter sentinel/placeholder (-1) indices before calling checkAndCoSort.","Audit index arithmetic for off-by-one and 0-vs-1 base errors.","Unit-test positions for non-negativity."],"tags":["csv","input-format","validation","api-misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}