apache/beam · error · NoSuchElementException

Empty PCollection accessed as a singleton view.

Error message

Empty PCollection accessed as a singleton view.

What it means

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.

Solutions

  1. Provide a default: View.asSingleton().withDefaultValue(someDefault) so empty inputs return the default instead of throwing.
  2. Guard the data: ensure the upstream PCollection is non-empty (e.g. add a sentinel element, or fail earlier with a clear message).
  3. Use a Map/List view (View.asMap() / View.asList()) if multiple or zero elements are legitimate, and check containsKey/isEmpty before use.

Example fix

// before
PCollectionView<Integer> view = pc.apply(View.asSingleton());
// after
PCollectionView<Integer> view = pc.apply(View.asSingleton().withDefaultValue(0));
Defensive patterns

Strategy: fallback

Validate before calling

// Cannot check emptiness locally; ensure non-empty upstream or:
PCollectionView<T> view = pc.apply(View.asSingleton().withDefaultValue(DEFAULT));

Try / catch

try {
  T value = c.sideInput(view);
} catch (NoSuchElementException e) {
  T value = DEFAULT; // empty side input, no default configured
}

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/3c16bfce99abe3b1. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java:518

    }

    /** Returns if a default value was specified. */
    @Internal
    public boolean hasDefault() {
      return hasDefault;
    }

    /**
     * Returns the default value that was specified.
     *
     * <p>For internal use only.
     *
     * @throws NoSuchElementException if no default was specified.
     */
    @Override
    public T getDefaultValue() {
      if (!hasDefault) {
        throw new NoSuchElementException("Empty PCollection accessed as a singleton view.");
      }
      // Lazily decode the default value once
      synchronized (this) {
        if (encodedDefaultValue != null) {
          try {
            defaultValue = CoderUtils.decodeFromByteArray(valueCoder, encodedDefaultValue);
            // Clear the encoded default value to free the reference once we have the object
            // version. Also, this will guarantee that the value will only be decoded once.
            encodedDefaultValue = null;
          } catch (IOException e) {
            throw new RuntimeException("Unexpected IOException: ", e);
          }
        }
        return defaultValue;
      }
    }

    @Override

View on GitHub (pinned to 12126d8942)