{"record":{"id":"768aac704cac05b3","repo":"apache/beam","slug":"cannot-encode-a-null-value-tabledestinationcoder","errorCode":null,"errorMessage":"cannot encode a null value","messagePattern":"cannot encode a null value","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TableDestinationCoder.java","lineNumber":46,"sourceCode":"import org.checkerframework.checker.nullness.qual.Nullable;\n\n/** A coder for {@link TableDestination} objects. */\npublic class TableDestinationCoder extends AtomicCoder<TableDestination> {\n  private static final TableDestinationCoder INSTANCE = new TableDestinationCoder();\n  private static final Coder<String> tableSpecCoder = StringUtf8Coder.of();\n  private static final Coder<@Nullable String> tableDescriptionCoder =\n      NullableCoder.of(StringUtf8Coder.of());\n\n  private TableDestinationCoder() {}\n\n  public static TableDestinationCoder of() {\n    return INSTANCE;\n  }\n\n  @Override\n  public void encode(TableDestination value, OutputStream outStream) throws IOException {\n    if (value == null) {\n      throw new CoderException(\"cannot encode a null value\");\n    }\n    tableSpecCoder.encode(value.getTableSpec(), outStream);\n    tableDescriptionCoder.encode(value.getTableDescription(), outStream);\n  }\n\n  @Override\n  public TableDestination decode(InputStream inStream) throws IOException {\n    String tableSpec = tableSpecCoder.decode(inStream);\n    String tableDescription = tableDescriptionCoder.decode(inStream);\n    return new TableDestination(tableSpec, tableDescription);\n  }\n\n  @Override\n  public void verifyDeterministic() throws NonDeterministicException {}\n}\n","sourceCodeStart":28,"sourceCodeEnd":62,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TableDestinationCoder.java#L28-L62","documentation":"TableDestinationCoder.encode rejects null values before serialization. Beam custom coders are required to throw a CoderException when handed a null, because the wire format has no representation for null TableDestinations. It means a null TableDestination reached a shuffle/encode point that only accepts fully-formed destinations.","triggerScenarios":"Calling TableDestinationCoder.INSTANCE.encode(null, outStream) directly, or a pipeline stage that emits/shuffles a null TableDestination (e.g. a DynamicDestinations returning null or a null key in a KV<TableDestination, ...> grouped write).","commonSituations":"Custom DynamicDestinations implementations that compute a table spec from records and return null when a field is missing; unit tests invoking encode with null to probe coder behavior.","solutions":["Fix the upstream code (e.g. DynamicDestinations.getTable) so it never returns null; return a valid TableDestination or fail with a descriptive error earlier.","Filter out records that would produce a null destination before the BigQueryIO write transform.","In direct coder use, check value == null before calling encode and decide on explicit handling."],"exampleFix":"// before\nTableDestination dest = dynamicDestinations.getTable(row); // may be null\ncontext.output(dest);\n// after\nTableDestination dest = dynamicDestinations.getTable(row);\nif (dest == null) {\n  throw new IllegalArgumentException(\"No destination resolved for record: \" + row);\n}\ncontext.output(dest);","handlingStrategy":"validation","validationCode":"if (dest == null) {\n  throw new IllegalArgumentException(\"TableDestination must not be null before encode/write\");\n}","typeGuard":"boolean isValidDestination(TableDestination d) { return d != null && d.getTableSpec() != null; }","tryCatchPattern":null,"preventionTips":["Make DynamicDestinations.getTable total: never return null, throw a descriptive error instead.","Filter records that cannot resolve to a destination before the BigQueryIO write."],"tags":["java","beam","bigquery","coder","null-check"],"backgroundTag":"null-argument","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"}