{"record":{"id":"66ea9c3d24cb0394","repo":"apache/beam","slug":"cannot-convert-value-of-type-value-null-value-getclass","errorCode":null,"errorMessage":"Cannot convert value of type \" + (value != null ? value.getClass().getName() : \"null\") + \" to Row","messagePattern":"Cannot convert value of type \" \\+ \\(value != null \\? value\\.getClass\\(\\)\\.getName\\(\\) : \"null\"\\) \\+ \" to Row","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbUtils.java","lineNumber":196,"sourceCode":"        FieldType valueType = fieldType.getMapValueType();\n        if (valueType == null) {\n          throw new IllegalArgumentException(\n              \"Map value type cannot be null for type: \" + fieldType);\n        }\n        for (Map.Entry<?, ?> entry : map.entrySet()) {\n          rowMap.put(\n              String.valueOf(entry.getKey()), convertFromBsonValue(entry.getValue(), valueType));\n        }\n        return rowMap;\n      case ROW:\n        Schema rowSchema = fieldType.getRowSchema();\n        if (rowSchema == null) {\n          throw new IllegalArgumentException(\"Row schema cannot be null for type: \" + fieldType);\n        }\n        if (value instanceof Map) {\n          return toRow((Map<?, ?>) value, rowSchema);\n        } else {\n          throw new IllegalArgumentException(\n              \"Cannot convert value of type \"\n                  + (value != null ? value.getClass().getName() : \"null\")\n                  + \" to Row\");\n        }\n      default:\n        throw new IllegalArgumentException(\"Unsupported field type: \" + fieldType);\n    }\n  }\n}\n","sourceCodeStart":178,"sourceCodeEnd":206,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbUtils.java#L178-L206","documentation":"The field is typed ROW and has a valid nested Schema, but the BSON-decoded value is neither null-handled as a Map nor convertible to a Row — it is some other Java class. toRow(Map, Schema) only works for Map-shaped values, so any other runtime type (String, List, ObjectId, etc.) is rejected.","triggerScenarios":"A MongoDB field declared as a nested struct in the Beam schema holds a non-document value (scalar, array, ObjectId, Date) when read; convertFromBsonValue reaches the ROW case with a non-Map object.","commonSituations":"Documents written with polymorphic/evolving field types (one doc has an object, another a string); schema inference ran on a different document than the one failing; DBRef or ObjectId stored where a sub-document is expected.","solutions":["Align the Beam schema with the actual stored type: use STRING/OBJECT_ID/ARRAY types for non-document values.","Clean or migrate the MongoDB collection so the field is consistently a sub-document.","Add a transform step that reshapes non-Map values into Maps matching the nested schema before toRow.","Make the schema tolerant: if the field can be either, store it as STRING and parse downstream."],"exampleFix":"// before: field inferred as ROW but data is an array\nField.of(\"tags\", FieldType.row(personSchema))\n// after\nField.of(\"tags\", FieldType.array(FieldType.STRING))","handlingStrategy":"type-guard","validationCode":"Object v = doc.get(fieldName);\nif (v != null && !(v instanceof Map) && schema.getField(fieldName).getType().getTypeName().equals(TypeName.ROW)) {\n  throw new IllegalStateException(fieldName + \" is not a sub-document: \" + v.getClass());\n}","typeGuard":"static boolean isSubDocument(Object v) { return v instanceof java.util.Map; }","tryCatchPattern":"try {\n  Row row = toRow(doc, schema);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().endsWith(\" to Row\")) {\n    LOG.warn(\"Skipping doc with non-document struct field: {}\", e.getMessage());\n    return null;\n  }\n  throw e;\n}","preventionTips":["Enforce consistent field types with MongoDB schema validation ($jsonSchema) on the collection.","Check a sample of documents across partitions when inferring schemas.","Handle polymorphic fields explicitly with oneOf-like STRING encoding."],"tags":["mongodb","apache-beam","schema","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}