dotnet/reactive · error · ArgumentNullException

ArgumentNullException

Error message

ArgumentNullException

What it means

AsQbservable wraps an IObservable<T> into an IQbservable<T> (queryable observable for expression-based LINQ providers). Because a null source cannot be wrapped, the extension method validates `source` and throws ArgumentNullException synchronously at the call site.

Solutions

  1. Null-check the sequence before calling AsQbservable.
  2. Fix the producer to return Observable.Empty<TSource>() instead of null.
  3. Use the null-coalescing operator: (source ?? Observable.Empty<TSource>()).AsQbservable().

Example fix

// before
var q = source.AsQbservable();
// after
var q = (source ?? Observable.Empty<int>()).AsQbservable();
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));

Type guard

static bool IsSource<T>(IObservable<T>? s) => s is not null;

Try / catch

try { var q = source.AsQbservable(); }
catch (ArgumentNullException ex) { logger.LogError(ex, "AsQbservable requires a non-null source"); }

Prevention

When it happens

Trigger: Calling source.AsQbservable() where source is null — e.g. a service property or method result that is null instead of an observable sequence.

Common situations: DI-injected stream properties not yet initialized; methods returning null instead of Observable.Empty; chaining off an optional sequence without null checks.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/77e28d6519bf0a3f. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Queryable.cs:44

            {
                s_provider ??= new ObservableQueryProvider();

                return s_provider;
            }
        }

        /// <summary>
        /// Converts an in-memory observable sequence into an <see cref="IQbservable{T}"/> sequence with an expression tree representing the source sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <returns><see cref="IQbservable{T}"/> sequence representing the given observable source sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        public static IQbservable<TSource> AsQbservable<TSource>(this IObservable<TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return new ObservableQuery<TSource>(source);
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)