{"record":{"id":"1ac16cf8b361b305","repo":"apache/beam","slug":"row-expected-fieldcount-fields-fields-initialized-with","errorCode":null,"errorMessage":"Row expected <fieldCount> fields (<fields>). initialized with <values.size> fields.","messagePattern":"Row expected <fieldCount> fields \\(<fields>\\)\\. initialized with <values\\.size> fields\\.","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/values/Row.java","lineNumber":852,"sourceCode":"\n    public int nextFieldId() {\n      return values.size();\n    }\n\n    @Internal\n    public <T> Row withFieldValueGetters(\n        Factory<List<FieldValueGetter<T, Object>>> fieldValueGetterFactory,\n        T getterTarget,\n        TypeDescriptor<?> getterTargetType) {\n      checkState(getterTarget != null, \"getters require withGetterTarget.\");\n      return new RowWithGetters<>(schema, fieldValueGetterFactory, getterTarget, getterTargetType);\n    }\n\n    public Row build() {\n      checkNotNull(schema);\n\n      if (!values.isEmpty() && values.size() != schema.getFieldCount()) {\n        throw new IllegalArgumentException(\n            \"Row expected \"\n                + schema.getFieldCount()\n                + String.format(\n                    \" fields (%s).\",\n                    schema.getFields().stream()\n                        .map(Object::toString)\n                        .collect(Collectors.joining(\", \")))\n                + \" initialized with \"\n                + values.size()\n                + \" fields.\");\n      }\n\n      if (!values.isEmpty()) {\n        FieldOverrides fieldOverrides = new FieldOverrides(schema, this.values);\n        if (!fieldOverrides.isEmpty()) {\n          return (Row)\n              new RowFieldMatcher()\n                  .match(","sourceCodeStart":834,"sourceCodeEnd":870,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/Row.java#L834-L870","documentation":"Row.Builder.build() verifies that the number of accumulated values equals the schema's field count. Mismatched value counts produce an IllegalArgumentException listing the expected fields and the supplied values. Rows are schema-checked records in Beam, so field count must match exactly (unless the builder is intentionally left empty for null-only rows).","triggerScenarios":"Calling Row.withSchema(...).addValue(...) a different number of times than schema.getFieldCount(); building a Row from a list whose size differs from the schema; schema evolved (fields added/removed) but row-construction code not updated.","commonSituations":"Schema drift after adding a field to an Avro/Beam schema without updating hand-built Rows; constructing Rows in tests with partial values; mapping a database record with NULL columns skipped instead of added as null values.","solutions":["Add exactly one value per schema field, using null for missing/NULL values.","Regenerate the schema from the current class (Schema.of / avro reflection) and align builder calls with it.","Use Row.withSchema(schema).addValues(...) with a list whose size equals schema.getFieldCount().","If fields were added to the schema, update all Row construction sites (search for withSchema usages)."],"exampleFix":"// before\nRow row = Row.withSchema(schema) // schema has 3 fields\n    .addValue(\"a\")\n    .addValue(1)\n    .build(); // IllegalArgumentException: expected 3 fields\n// after\nRow row = Row.withSchema(schema)\n    .addValue(\"a\")\n    .addValue(1)\n    .addValue(null) // third field\n    .build();","handlingStrategy":"validation","validationCode":"if (values.size() != schema.getFieldCount()) {\n  throw new IllegalArgumentException(\"Row needs \" + schema.getFieldCount() + \" values, got \" + values.size());\n}","typeGuard":"static boolean matchesSchema(List<Object> values, Schema schema) {\n  return values == null ? schema.getFieldCount() == 0 : values.size() == schema.getFieldCount();\n}","tryCatchPattern":"try {\n  Row row = builder.build();\n} catch (IllegalArgumentException e) {\n  throw new IllegalStateException(\"Row/Schema field count mismatch: \" + e.getMessage(), e);\n}","preventionTips":["Derive Row values from the schema fields programmatically instead of hardcoding.","Add null values for missing fields rather than skipping them.","Regenerate Row-building code whenever the schema changes.","Validate values.size() == schema.getFieldCount() before build()."],"tags":["java","apache-beam","row","schema","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}