{"record":{"id":"6352e5bac3036528","repo":"apache/beam","slug":"cannot-encode-null-window","errorCode":null,"errorMessage":"Cannot encode null window","messagePattern":"Cannot encode null window","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/TimestampPrefixingWindowCoder.java","lineNumber":54,"sourceCode":"  private final Coder<T> windowCoder;\n\n  public static <T extends BoundedWindow> TimestampPrefixingWindowCoder<T> of(\n      Coder<T> windowCoder) {\n    return new TimestampPrefixingWindowCoder<>(windowCoder);\n  }\n\n  TimestampPrefixingWindowCoder(Coder<T> windowCoder) {\n    this.windowCoder = windowCoder;\n  }\n\n  public Coder<T> getWindowCoder() {\n    return windowCoder;\n  }\n\n  @Override\n  public void encode(T value, OutputStream outStream) throws CoderException, IOException {\n    if (value == null) {\n      throw new CoderException(\"Cannot encode null window\");\n    }\n    InstantCoder.of().encode(value.maxTimestamp(), outStream);\n    windowCoder.encode(value, outStream);\n  }\n\n  @Override\n  public T decode(InputStream inStream) throws CoderException, IOException {\n    InstantCoder.of().decode(inStream);\n    return windowCoder.decode(inStream);\n  }\n\n  @Override\n  public List<? extends Coder<?>> getCoderArguments() {\n    return Lists.newArrayList(windowCoder);\n  }\n\n  @Override\n  public void verifyDeterministic() throws NonDeterministicException {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/TimestampPrefixingWindowCoder.java#L36-L72","documentation":"TimestampPrefixingWindowCoder wraps a window coder and prefixes the window's maximum timestamp; null windows are not encodable, so encode throws CoderException. Neither the timestamp prefix nor the delegated windowCoder has a defined null representation.","triggerScenarios":"Calling encode(null, out) directly; windowed data structures containing a null window slot being written out; custom WindowedValue serialization with a missing window field.","commonSituations":"Custom code or test harnesses building WindowedValue instances with the window field left unset; deserialized state that lost its window and is re-encoded for shuffle/state; windowing logic that never called Window.into().","solutions":["Ensure every element carries a real BoundedWindow (use GlobalWindow for unwindowed data).","Apply Window.into(...) so all elements get an assigned window.","In custom code, default null windows to GlobalWindow.INSTANCE before encoding.","Check state restored from old checkpoints for missing window fields."],"exampleFix":"// before\nprefixingCoder.encode(maybeNullWindow, out);\n// after\nBoundedWindow w = maybeNullWindow != null ? maybeNullWindow : GlobalWindow.INSTANCE;\nprefixingCoder.encode((T) w, out);","handlingStrategy":"validation","validationCode":"if (window == null) {\n  window = GlobalWindow.INSTANCE; // or throw IllegalArgumentException\n}","typeGuard":"static boolean hasWindow(BoundedWindow w) { return w != null; }","tryCatchPattern":"try {\n  prefixingCoder.encode(window, out);\n} catch (CoderException e) {\n  throw new SerializationException(\"Null window in windowed value\", e);\n}","preventionTips":["Apply Window.into(...) so every element has an assigned window.","Default unwindowed data to GlobalWindow.INSTANCE.","Never leave window fields unset when constructing WindowedValue values.","Verify re-serialized state from checkpoints retains window data."],"tags":["java","beam","coder","windowing","null-pointer"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}