dotnet/reactive · error · ArgumentNullException
ArgumentNullException (default message: Value cannot be…
Error message
ArgumentNullException (default message: Value cannot be null. (Parameter 'source'))
What it means
System.Reactive's Cast operator requires a non-null source observable. The public API surface performs eager argument validation and throws ArgumentNullException before any subscription happens, matching Rx's documented contract that operator inputs must not be null.
Solutions
- Ensure the observable passed to Cast<TResult> is initialized before calling the operator
- Add an explicit null check (ArgumentNullException.ThrowIfNull(source) or if (source == null)) before the call
- If null means 'no stream', substitute Observable.Empty<object>() instead of null
- Verify the code path that assigns the source actually ran (DI registration, async init)
Example fix
// before IObservable<object> src = _lazyStream?.Value; // may be null var result = src.Cast<int>(); // after var result = (_lazyStream?.Value ?? Observable.Empty<object>()).Cast<int>();
Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new ArgumentNullException(nameof(source)); // or: ArgumentNullException.ThrowIfNull(source);
Type guard
bool HasSource(IObservable<object>? source) => source is not null;
Try / catch
try { var result = source.Cast<int>(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
// use fallback stream or log
result = Observable.Empty<int>();
} Prevention
- Never return null IObservable from factory methods; return Observable.Empty<T>()
- Null-check observables at construction/boundary, not at every operator call
- Prefer nullable reference types (IObservable<T>?) so the compiler flags risky passes
- Use ArgumentNullException.ThrowIfNull early in your own pipeline builders
When it happens
Trigger: Calling Observable.Cast<TResult>(null) — i.e., passing a null IObservable<object> as the source argument.
Common situations: A field or factory method returning an observable is null (e.g., a service not yet initialized), or a nullable variable is passed without a null check before chaining LINQ-style Rx operators.
Related errors
- ArgumentNullException (default message: Value cannot be…
- ArgumentNullException (default message: Value cannot be…
- first
- keySelector
- scheduler
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/ae334733ecdbf646.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs:28
namespace System.Reactive.Linq
{
public static partial class Observable
{
#region + Cast +
/// <summary>
/// Converts the elements of an observable sequence to the specified type.
/// </summary>
/// <typeparam name="TResult">The type to convert the elements in the source sequence to.</typeparam>
/// <param name="source">The observable sequence that contains the elements to be converted.</param>
/// <returns>An observable sequence that contains each element of the source sequence converted to the specified type.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IObservable<TResult> Cast<TResult>(this IObservable<object> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return s_impl.Cast<TResult>(source);
}
#endregion
#region + DefaultIfEmpty +
/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton sequence if the sequence is empty.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence (if any), whose default value will be taken if the sequence is empty.</typeparam>
/// <param name="source">The sequence to return a default value for if it is empty.</param>
/// <returns>An observable sequence that contains the default value for the TSource type if the source is empty; otherwise, the elements of the source itself.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IObservable<TSource?> DefaultIfEmpty<TSource>(this IObservable<TSource> source)
{View on GitHub (pinned to 94b5d5ab91)