apache/flink · error · IllegalArgumentException

Field indices must not be smaller than zero.

Error message

Field indices must not be smaller than zero.

What it means

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.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java:238

                }
                types.add(type);
                fieldIncluded[i] = true;
            }
        }

        this.fieldTypes = types.toArray(new Class<?>[types.size()]);
    }

    protected void setFieldsGeneric(int[] sourceFieldIndices, Class<?>[] fieldTypes) {
        checkNotNull(sourceFieldIndices);
        checkNotNull(fieldTypes);
        checkArgument(
                sourceFieldIndices.length == fieldTypes.length,
                "Number of field indices and field types must match.");

        for (int i : sourceFieldIndices) {
            if (i < 0) {
                throw new IllegalArgumentException("Field indices must not be smaller than zero.");
            }
        }

        int largestFieldIndex = max(sourceFieldIndices);
        this.fieldIncluded = new boolean[largestFieldIndex + 1];
        ArrayList<Class<?>> types = new ArrayList<Class<?>>();

        // check if we support parsers for these types
        for (int i = 0; i < fieldTypes.length; i++) {
            Class<?> type = fieldTypes[i];

            if (type != null) {
                if (FieldParser.getParserForType(type) == null) {
                    throw new IllegalArgumentException(
                            "The type '"
                                    + type.getName()
                                    + "' is not supported for the CSV input format.");
                }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Validate that every index is >= 0 before calling; replace any -1 sentinel with a real column or a null type to skip.
  2. Add an assertion: assert Arrays.stream(indices).allMatch(i -> i >= 0).
  3. Review index arithmetic for underflow (subtraction, modulo on negative inputs).

Example fix

// before
int[] indices = {0, indexOf(col) /* -1 when not found */};
format.setFieldsGeneric(indices, types);

// after
int[] indices = {0, Math.max(0, indexOf(col))};
format.setFieldsGeneric(indices, types);
Defensive patterns

Strategy: validation

Validate before calling

for (int idx : sourceFieldIndices) {
    if (idx < 0) {
        throw new IllegalArgumentException("field index must be >= 0, got " + idx);
    }
}
format.setFieldsGeneric(sourceFieldIndices, fieldTypes);

Type guard

static boolean allIndicesNonNegative(int[] indices) {
    return indices != null && Arrays.stream(indices).allMatch(i -> i >= 0);
}

Prevention

When it happens

Trigger: 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'.

Common situations: 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.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/2efe2d924a028a39. Report an issue: GitHub.