{"record":{"id":"3c16bfce99abe3b1","repo":"apache/beam","slug":"empty-pcollection-accessed-as-a-singleton-view","errorCode":null,"errorMessage":"Empty PCollection accessed as a singleton view.","messagePattern":"Empty PCollection accessed as a singleton view\\.","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java","lineNumber":518,"sourceCode":"    }\n\n    /** Returns if a default value was specified. */\n    @Internal\n    public boolean hasDefault() {\n      return hasDefault;\n    }\n\n    /**\n     * Returns the default value that was specified.\n     *\n     * <p>For internal use only.\n     *\n     * @throws NoSuchElementException if no default was specified.\n     */\n    @Override\n    public T getDefaultValue() {\n      if (!hasDefault) {\n        throw new NoSuchElementException(\"Empty PCollection accessed as a singleton view.\");\n      }\n      // Lazily decode the default value once\n      synchronized (this) {\n        if (encodedDefaultValue != null) {\n          try {\n            defaultValue = CoderUtils.decodeFromByteArray(valueCoder, encodedDefaultValue);\n            // Clear the encoded default value to free the reference once we have the object\n            // version. Also, this will guarantee that the value will only be decoded once.\n            encodedDefaultValue = null;\n          } catch (IOException e) {\n            throw new RuntimeException(\"Unexpected IOException: \", e);\n          }\n        }\n        return defaultValue;\n      }\n    }\n\n    @Override","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java#L500-L536","documentation":"SingletonViewFn2.getDefaultValue throws NoSuchElementException when get() is called on a singleton view backed by an empty PCollection and no default value was specified. Beam cannot return any element for an empty input, so it reports 'Empty PCollection accessed as a singleton view.' The javadoc explicitly documents this @throws condition.","triggerScenarios":"Calling sideInput(PCollectionViews.singletonView(...)) / pcoll.apply(View.asSingleton()) and then accessing the view via sideInput() at runtime when the input PCollection is empty and View.asSingleton() was used without withDefaultValue().","commonSituations":"Filtering upstream leaves zero matches (empty side input) — e.g. a lookup table filtered by a date with no data; tests where the input was left empty; pipelines that assume at least one record without verifying it.","solutions":["Provide a default: View.asSingleton().withDefaultValue(someDefault) so empty inputs return the default instead of throwing.","Guard the data: ensure the upstream PCollection is non-empty (e.g. add a sentinel element, or fail earlier with a clear message).","Use a Map/List view (View.asMap() / View.asList()) if multiple or zero elements are legitimate, and check containsKey/isEmpty before use."],"exampleFix":"// before\nPCollectionView<Integer> view = pc.apply(View.asSingleton());\n// after\nPCollectionView<Integer> view = pc.apply(View.asSingleton().withDefaultValue(0));","handlingStrategy":"fallback","validationCode":"// Cannot check emptiness locally; ensure non-empty upstream or:\nPCollectionView<T> view = pc.apply(View.asSingleton().withDefaultValue(DEFAULT));","typeGuard":null,"tryCatchPattern":"try {\n  T value = c.sideInput(view);\n} catch (NoSuchElementException e) {\n  T value = DEFAULT; // empty side input, no default configured\n}","preventionTips":["Always call withDefaultValue() when emptiness is possible.","Use View.asMap()/asList() when zero elements is a normal outcome.","Add an upstream count assertion in staging to catch unexpectedly empty side inputs early."],"tags":["java","apache-beam","empty-collection","no-such-element"],"backgroundTag":"empty-result-set","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"}