{"record":{"id":"915dc97c32f903b6","repo":"apache/beam","slug":"cannot-call-window-in-a-null-context","errorCode":null,"errorMessage":"cannot call window() in a null context","messagePattern":"cannot call window\\(\\) in a null context","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateContexts.java","lineNumber":42,"sourceCode":"\n/** <b><i>For internal use only; no backwards-compatibility guarantees.</i></b> */\n@Internal\npublic class StateContexts {\n  private static final StateContext<BoundedWindow> NULL_CONTEXT =\n      new StateContext<BoundedWindow>() {\n        @Override\n        public PipelineOptions getPipelineOptions() {\n          throw new IllegalArgumentException(\"cannot call getPipelineOptions() in a null context\");\n        }\n\n        @Override\n        public <T> T sideInput(PCollectionView<T> view) {\n          throw new IllegalArgumentException(\"cannot call sideInput() in a null context\");\n        }\n\n        @Override\n        public BoundedWindow window() {\n          throw new IllegalArgumentException(\"cannot call window() in a null context\");\n        }\n      };\n\n  /** Returns a fake {@link StateContext}. */\n  @SuppressWarnings(\"unchecked\")\n  public static <W extends BoundedWindow> StateContext<W> nullContext() {\n    return (StateContext<W>) NULL_CONTEXT;\n  }\n\n  public static <W extends BoundedWindow> StateContext<W> windowOnlyContext(W window) {\n    return new WindowOnlyContext<>(window);\n  }\n\n  private static class WindowOnlyContext<W extends BoundedWindow> implements StateContext<W> {\n    private final W window;\n\n    private WindowOnlyContext(W window) {\n      this.window = window;","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateContexts.java#L24-L60","documentation":"window() on StateContexts.NULL_CONTEXT always throws: the current BoundedWindow is only meaningful while processing an element, and this null context is a stand-in used when state is touched with no execution context bound. The faulty input is the call site invoking window() outside element processing.","triggerScenarios":"Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateContexts.java:42 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call window() from within @ProcessElement/@OnTimer where the context carries the current window.","Pass the window in explicitly to helper code instead of reading it from a context object that may be null.","Avoid capturing a StateContext before execution begins; obtain it from the DoFn's context parameters."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}