{"record":{"id":"327cac133bcf9e1b","repo":"apache/beam","slug":"recordwriter-is-null","errorCode":null,"errorMessage":"RecordWriter is null","messagePattern":"RecordWriter is null","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/KinesisIO.java","lineNumber":865,"sourceCode":"                  writer().write(record);\n                }\n\n                @FinishBundle\n                public void finishBundle() throws Throwable {\n                  writer().finishBundle();\n                }\n\n                @Teardown\n                public void teardown() throws Exception {\n                  if (writer != null) {\n                    writer.close();\n                    writer = null;\n                  }\n                }\n\n                private Writer<T> writer() {\n                  if (writer == null) {\n                    throw new IllegalStateException(\"RecordWriter is null\");\n                  }\n                  return writer;\n                }\n              }));\n      return new Result(input.getPipeline());\n    }\n\n    /** Result of {@link KinesisIO#write()}. */\n    public static class Result implements POutput {\n      private final Pipeline pipeline;\n\n      private Result(Pipeline pipeline) {\n        this.pipeline = pipeline;\n      }\n\n      @Override\n      public Pipeline getPipeline() {\n        return pipeline;","sourceCodeStart":847,"sourceCodeEnd":883,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/KinesisIO.java#L847-L883","documentation":"In KinesisIO.Write, the Writer instance used to publish records is created lazily (per-bundle). The writer() accessor throws IllegalStateException if a record is written before the writer was initialized or after it was closed and reset to null. This indicates a lifecycle bug: write() invoked outside the valid writer window.","triggerScenarios":"Writing a record in a DoFn lifecycle phase where the bundle writer hasn't been started (before @StartBundle) or was already torn down (@FinishBundle set writer = null), or a custom Writer supplier returned null.","commonSituations":"Custom DoFn code calling writer() in @ProcessElement with a misconfigured transform; pipeline reuse across bundles with a Writer factory that fails silently; subclassing the sink incorrectly.","solutions":["Ensure the transform is expanded and applied normally (KinesisIO.write().expandTo(input)) rather than invoking internal Write internals directly","Check that the Writer supplier/factory always returns a non-null Writer","Verify no custom pipeline patching skips @StartBundle initialization","Upgrade the Beam SDK if a known bundle-lifecycle bug is suspected"],"exampleFix":"// before\nSupplier<Writer<T>> supplier = () -> null; // or writer created outside bundle\n// after\nKinesisIO.<T>write().withBatchMaxCount(500).withWriterSupplier(() -> new KinesisWriter<>(...)); // non-null writer per bundle","handlingStrategy":"validation","validationCode":"// assert writer lifecycle: writer must be created in @StartBundle and only used in @ProcessElement","typeGuard":"if (writer == null) throw new IllegalStateException(\"writer not initialized for this bundle\");","tryCatchPattern":null,"preventionTips":["Use the standard KinesisIO.write() expansion, don't bypass bundle lifecycle","Ensure Writer suppliers never return null","Test with a small bounded pipeline before production"],"tags":["kinesis","state-error","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}