apache/flink · error · InvalidFieldReferenceException
Unable to find field "{}" in type {}.
Error message
Unable to find field "{}" in type {}. What it means
Thrown by PojoTypeInfo.getFlatFields when the fieldExpression matched the name regex but no PojoField with that exact name exists in the POJO. The expression syntax is valid but the field is simply not present. This is an InvalidFieldReferenceException.
Source
Thrown at flink-core/src/main/java/org/apache/flink/api/java/typeutils/PojoTypeInfo.java:200
keyPosition++;
}
return;
} else {
field = matcher.group(1);
}
// get field
int fieldPos = -1;
TypeInformation<?> fieldType = null;
for (int i = 0; i < fields.length; i++) {
if (fields[i].getField().getName().equals(field)) {
fieldPos = i;
fieldType = fields[i].getTypeInformation();
break;
}
}
if (fieldPos == -1) {
throw new InvalidFieldReferenceException(
"Unable to find field \"" + field + "\" in type " + this + ".");
}
String tail = matcher.group(3);
if (tail == null) {
if (fieldType instanceof CompositeType) {
// forward offset
for (int i = 0; i < fieldPos; i++) {
offset += this.getTypeAt(i).getTotalFields();
}
// add all fields of composite type
((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
} else {
// we found the field to add
// compute flat field position by adding skipped fields
int flatFieldPos = offset;
for (int i = 0; i < fieldPos; i++) {
flatFieldPos += this.getTypeAt(i).getTotalFields();
}View on GitHub (pinned to 2f3c205e92)
Solutions
- Verify the exact field name spelling and case against the POJO class definition.
- Ensure the class satisfies POJO requirements: public class, public no-arg constructor, and either public fields or public getter/setter for the field.
- Print the resolved type with System.out.println(TypeInformation.of(MyPojo.class)) and check it says 'PojoType' with the expected fields.
- If the field was renamed, update all keyBy/groupBy references.
Example fix
// before
public class Event { private String userName; }
stream.keyBy("usrName");
// after
public class Event {
public String userName; // or getter+setter
}
stream.keyBy("userName"); Defensive patterns
Strategy: validation
Validate before calling
List<String> pojoFieldNames =
((PojoTypeInfo<?>) TypeInformation.of(MyPojo.class))
.getPojoFields().stream()
.map(f -> f.getField().getName())
.collect(Collectors.toList());
if (!pojoFieldNames.contains(keyField)) {
throw new IllegalArgumentException(
"Field '" + keyField + "' not in POJO. Available: " + pojoFieldNames);
} Prevention
- Log or print TypeInformation.of(MyPojo.class) during development to confirm the POJO fields Flink recognizes.
- Ensure POJO classes have public fields or getter/setter pairs so Flink detects all fields.
- Use a centralized constants class for field names.
When it happens
Trigger: Calling dataStream.keyBy("usrName") when the POJO field is named "userName", or keyBy("nonExistent"). Also triggered by pojoTypeInfo.getFlatFields("missingField"). The lookup loop at line 192-198 iterates fields[] and never finds a name match, leaving fieldPos == -1.
Common situations: Typo or case mismatch in the key string. The field is private and Flink did not recognize the class as a valid POJO (no public field or getter/setter pair), so the field list is incomplete. The class fell back to being treated as a generic type or POJO with fewer fields than expected after a refactor or rename.
Related errors
- Invalid POJO field reference "{}".
- Invalid format of POJO field expression "{}".
- Invalid tuple field reference "{}".
- This type ({field.getType()}) cannot be used as key.
- This type ({type}) cannot be used as key.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/962de8f29570ce14.
Report an issue: GitHub.