dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

Qbservable.AsObservable(IQbservable<TSource>) throws ArgumentNullException when the source qbservable is null. AsObservable merely re-types the source as IObservable<TSource>, so it has no value to return when the input is null; the guard exists to fail fast with a clear message instead of a NullReferenceException later.

Solutions

  1. Ensure the upstream call that produced the IQbservable<TSource> never returns null; fix that producer to throw or return a non-null empty qbservable.
  2. Check the source for null before calling AsObservable.
  3. If the value is legitimately optional, guard with source?.AsObservable() and handle the null result explicitly.

Example fix

// before
IQbservable<int> source = GetSource(); // may return null
var obs = source.AsObservable();
// after
IQbservable<int> source = GetSource() ?? Qbservable.Empty<int>(provider);
var obs = source.AsObservable();
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new InvalidOperationException("Cannot call AsObservable on a null qbservable.");

Type guard

static bool IsNonNull<T>(IQbservable<T> q) => q is not null;

Try / catch

try { var obs = source.AsObservable(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* handle missing source */ }

Prevention

When it happens

Trigger: Calling source.AsObservable() where source is a null IQbservable<TSource> reference — e.g. the result of a method that returned null, or an uninitialized field passed to the static form Qbservable.AsObservable(null).

Common situations: Factory methods or repository lookups returning null qbservables; null-conditional chains elsewhere that silently produced null; tests using null placeholders for observables.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs:30

{
    /// <summary>
    /// Provides a set of static methods for writing queries over observable sequences, allowing translation to a target query language.
    /// </summary>
    public static partial class Qbservable
    {
        /// <summary>
        /// Returns the input typed as an <see cref="IObservable{TSource}"/>.
        /// This operator is used to separate the part of the query that's captured as an expression tree from the part that's executed locally.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">An <see cref="IQbservable{TSource}"/> sequence to convert to an <see cref="IObservable{TSource}"/> sequence.</param>
        /// <returns>The original source object, but typed as an <see cref="IObservable{TSource}"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        public static IObservable<TSource> AsObservable<TSource>(this IQbservable<TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return source;
        }

        /// <summary>
        /// Converts an enumerable sequence to an observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
        /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <remarks>This operator requires the source's <see cref="IQueryProvider"/> object (see <see cref="IQueryable.Provider"/>) to implement <see cref="IQbservableProvider"/>.</remarks>
        public static IQbservable<TSource> ToQbservable<TSource>(this IQueryable<TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));

View on GitHub (pinned to 94b5d5ab91)