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
- 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.
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
- 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.
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
- must either have a non-empty list of PCollections, or must…
- A function must be provided to convert the input type into…
- A PValue contained in
- A schema was provided without a data format (or viceversa)…
- All inherited interfaces of
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;
}
}
@OverrideView on GitHub (pinned to 12126d8942)