{"record":{"id":"71d25e538a6da519","repo":"apache/beam","slug":"cannot-encode-a-null-value-filebasedsink","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/core/src/main/java/org/apache/beam/sdk/io/FileBasedSink.java","lineNumber":1204,"sourceCode":"    public static <DestinationT> FileResultCoder<DestinationT> of(\n        Coder<BoundedWindow> windowCoder, Coder<DestinationT> destinationCoder) {\n      return new FileResultCoder<>(windowCoder, destinationCoder);\n    }\n\n    @Override\n    public List<? extends Coder<?>> getCoderArguments() {\n      return Collections.singletonList(destinationCoder);\n    }\n\n    @Override\n    public List<? extends Coder<?>> getComponents() {\n      return Arrays.asList(windowCoder, destinationCoder);\n    }\n\n    @Override\n    public void encode(FileResult<DestinationT> value, OutputStream outStream) throws IOException {\n      if (value == null) {\n        throw new CoderException(\"cannot encode a null value\");\n      }\n      FILENAME_CODER.encode(value.getTempFilename().toString(), outStream);\n      windowCoder.encode(value.getWindow(), outStream);\n      PANE_INFO_CODER.encode(value.getPaneInfo(), outStream);\n      SHARD_CODER.encode(value.getShard(), outStream);\n      destinationCoder.encode(value.getDestination(), outStream);\n    }\n\n    @Override\n    public FileResult<DestinationT> decode(InputStream inStream) throws IOException {\n      String tempFilename = FILENAME_CODER.decode(inStream);\n      BoundedWindow window = windowCoder.decode(inStream);\n      PaneInfo paneInfo = PANE_INFO_CODER.decode(inStream);\n      int shard = SHARD_CODER.decode(inStream);\n      DestinationT destination = destinationCoder.decode(inStream);\n      return new FileResult<>(\n          FileSystems.matchNewResource(tempFilename, false /* isDirectory */),\n          shard,","sourceCodeStart":1186,"sourceCodeEnd":1222,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileBasedSink.java#L1186-L1222","documentation":"FileResult.Coder.encode() throws CoderException when asked to encode a null FileResult value. Beam coders must reject nulls explicitly, so a null reaching the coder indicates an upstream bug or missing element filtering.","triggerScenarios":"A null FileResult element entering a codable step (e.g. WriteFiles/DynamicDestinations result PCollections), typically from user DoFns emitting null or buggy group/key extraction.","commonSituations":"Custom pipelines that build FileResult lists manually, or joins/coGroup producing null outputs passed onward to write steps.","solutions":["Find and fix the upstream step emitting null FileResult values (filter with null checks before coding).","Ensure sink Writer operations never return null results.","If nulls are expected, filter them: filter(Objects::nonNull) before the coding step."],"exampleFix":"// before\nresults.add(null);\n// after\nif (result != null) { results.add(result); }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"boolean isNonNullFileResult(FileResult<?> r) { return r != null; }","tryCatchPattern":"try { coder.encode(value, out); } catch (CoderException e) { throw new IllegalStateException(\"Null or malformed FileResult reached coder — check upstream DoFns\", e); }","preventionTips":["Filter nulls before any coded step: .filter(Objects::nonNull).","Assert Writer implementations never return null results in unit tests."],"tags":["java","apache-beam","coder","null"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}