{"record":{"id":"ce816772a88fd0de","repo":"apache/beam","slug":"unexpected-null-element-type","errorCode":null,"errorMessage":"Unexpected null element type!","messagePattern":"Unexpected null element type!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java","lineNumber":478,"sourceCode":"      } else {\n        throw new IllegalArgumentException(\n            \"Received null value for non-nullable field \" + fieldDescriptor.getName());\n      }\n    }\n    return toProtoValue(fieldDescriptor, avroField.schema(), value);\n  }\n\n  private static Object toProtoValue(\n      FieldDescriptor fieldDescriptor, Schema avroSchema, Object value) {\n    switch (avroSchema.getType()) {\n      case RECORD:\n        return messageFromGenericRecord(\n            fieldDescriptor.getMessageType(), (GenericRecord) value, null, -1);\n      case ARRAY:\n        Iterable<Object> iterable = (Iterable<Object>) value;\n        @Nullable Schema arrayElementType = avroSchema.getElementType();\n        if (arrayElementType == null) {\n          throw new RuntimeException(\"Unexpected null element type!\");\n        }\n        return StreamSupport.stream(iterable.spliterator(), false)\n            .map(v -> toProtoValue(fieldDescriptor, arrayElementType, v))\n            .collect(Collectors.toList());\n      case UNION:\n        TypeWithNullability type = TypeWithNullability.create(avroSchema);\n        Preconditions.checkState(\n            type.getType().getType() != Schema.Type.UNION,\n            \"Multiple non-null union types are not supported.\");\n        return toProtoValue(fieldDescriptor, type.getType(), value);\n      case MAP:\n        Map<CharSequence, Object> map = (Map<CharSequence, Object>) value;\n        Schema valueType = Schema.create(avroSchema.getValueType().getType());\n        if (valueType == null) {\n          throw new RuntimeException(\"Unexpected null element type!\");\n        }\n\n        return map.entrySet().stream()","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java#L460-L496","documentation":"Thrown by toProtoValue in AvroGenericRecordToStorageApiProto when converting an Avro ARRAY-typed field to a BigQuery Storage API protobuf message. The code reads the Avro schema's element type via avroSchema.getElementType() and throws a RuntimeException if it is null, since array elements cannot be converted without a known element schema. This indicates the Avro schema for the field is not actually an array with a resolvable element type.","triggerScenarios":"Processing a GenericRecord whose field is declared ARRAY in the branch logic but whose underlying Avro Schema returns null from getElementType() — e.g. a malformed or hand-constructed Avro Schema passed to write() of BigQueryIO with method=STORAGE_API_DIRECT","commonSituations":"Programmatically built Avro schemas (SchemaBuilder misuse), deserialization edge cases where nested schemas get stripped, or feeding data whose runtime schema doesn't match the schema used to build the BigQuery table schema.","solutions":["Verify the Avro schema's array fields are proper array schemas (schema.getField(name).schema().getType() == ARRAY) before writing","Rebuild the schema with SchemaBuilder.array().items(...) so getElementType() is non-null","Inspect the specific field's schema at runtime and log avroSchema before conversion to find the mismatch","Update/align your Beam and google-cloud-libraries versions — some schema handling bugs were fixed in later releases"],"exampleFix":"// before\nSchema arraySchema = Schema.create(Schema.Type.ARRAY); // no element type\n// after\nSchema arraySchema = Schema.createArray(Schema.create(Schema.Type.STRING));","handlingStrategy":"validation","validationCode":"// Java\nfor (Field f : avroSchema.getFields()) {\n  if (f.schema().getType() == Schema.Type.ARRAY && f.schema().getElementType() == null) {\n    throw new IllegalStateException(\"Array field without element type: \" + f.name());\n  }\n}","typeGuard":"// Java\nSchema et = avroSchema.getElementType();\nif (et != null) { /* safe to map elements */ }","tryCatchPattern":"// Java\ntry {\n  return toProtoValue(fieldDescriptor, arrayElementType, value);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"Unexpected null element type\")) {\n    throw new IllegalStateException(\"Malformed Avro array schema for field \" + fieldDescriptor.getName(), e);\n  }\n  throw e;\n}","preventionTips":["Always build array schemas with Schema.createArray(...) or SchemaBuilder.array().items(...)","Validate the Avro schema once at pipeline construction time, not per record","Keep data schema and declared table schema in one shared definition"],"tags":["java","bigquery","avro","null-schema"],"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-20T03:17:13.778Z"}