{"record":{"id":"2efe2d924a028a39","repo":"apache/flink","slug":"field-indices-must-not-be-smaller-than-zero","errorCode":null,"errorMessage":"Field indices must not be smaller than zero.","messagePattern":"Field indices must not be smaller than zero\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java","lineNumber":238,"sourceCode":"                }\n                types.add(type);\n                fieldIncluded[i] = true;\n            }\n        }\n\n        this.fieldTypes = types.toArray(new Class<?>[types.size()]);\n    }\n\n    protected void setFieldsGeneric(int[] sourceFieldIndices, Class<?>[] fieldTypes) {\n        checkNotNull(sourceFieldIndices);\n        checkNotNull(fieldTypes);\n        checkArgument(\n                sourceFieldIndices.length == fieldTypes.length,\n                \"Number of field indices and field types must match.\");\n\n        for (int i : sourceFieldIndices) {\n            if (i < 0) {\n                throw new IllegalArgumentException(\"Field indices must not be smaller than zero.\");\n            }\n        }\n\n        int largestFieldIndex = max(sourceFieldIndices);\n        this.fieldIncluded = new boolean[largestFieldIndex + 1];\n        ArrayList<Class<?>> types = new ArrayList<Class<?>>();\n\n        // check if we support parsers for these types\n        for (int i = 0; i < fieldTypes.length; i++) {\n            Class<?> type = fieldTypes[i];\n\n            if (type != null) {\n                if (FieldParser.getParserForType(type) == null) {\n                    throw new IllegalArgumentException(\n                            \"The type '\"\n                                    + type.getName()\n                                    + \"' is not supported for the CSV input format.\");\n                }","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java#L220-L256","documentation":"Thrown by GenericCsvInputFormat.setFieldsGeneric(int[] sourceFieldIndices, Class<?>[] fieldTypes) when any entry in sourceFieldIndices is negative. Field indices are 0-based CSV column positions; negative indices have no meaning and would produce an invalid fieldIncluded mask.","triggerScenarios":"Passing a sourceFieldIndices array containing a value < 0; computing indices from a formula that underflows (e.g., a - b where b > a); an off-by-one that yields -1 for 'not found'.","commonSituations":"Programmatic schema mapping where an index is derived from a lookup that returned -1 (not-found sentinel) instead of being guarded; user input parsed as a negative column number.","solutions":["Validate that every index is >= 0 before calling; replace any -1 sentinel with a real column or a null type to skip.","Add an assertion: assert Arrays.stream(indices).allMatch(i -> i >= 0).","Review index arithmetic for underflow (subtraction, modulo on negative inputs)."],"exampleFix":"// before\nint[] indices = {0, indexOf(col) /* -1 when not found */};\nformat.setFieldsGeneric(indices, types);\n\n// after\nint[] indices = {0, Math.max(0, indexOf(col))};\nformat.setFieldsGeneric(indices, types);","handlingStrategy":"validation","validationCode":"for (int idx : sourceFieldIndices) {\n    if (idx < 0) {\n        throw new IllegalArgumentException(\"field index must be >= 0, got \" + idx);\n    }\n}\nformat.setFieldsGeneric(sourceFieldIndices, fieldTypes);","typeGuard":"static boolean allIndicesNonNegative(int[] indices) {\n    return indices != null && Arrays.stream(indices).allMatch(i -> i >= 0);\n}","tryCatchPattern":null,"preventionTips":["Guard lookup-based indices against -1 sentinels before use.","Add an assertion that every index is >= 0.","Review subtraction/modulo arithmetic that may underflow."],"tags":["flink","csv","input-format","validation","field-index"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}