PrismLibrary/Prism · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

Prism's ObservableExtensions.Subscribe extension performs an explicit null check on the IObservable<T> source before subscribing and throws ArgumentNullException(nameof(source)) when it is null. This guards the extension method against being invoked on a null receiver.

Solutions

  1. Ensure the IObservable<T> source is initialized before calling Subscribe (constructor/property initialization).
  2. Null-check the source or use source?.Subscribe(...) if null is a valid state you want to skip.
  3. Fix the factory/provider that returns null instead of a non-null (possibly empty) observable.

Example fix

// before
_stream.Subscribe(OnNext); // _stream may be null
// after
_stream ??= Observable.Empty<int>();
_stream.Subscribe(OnNext);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new InvalidOperationException("Observable source not initialized");
source.Subscribe(onNext);

Type guard

IObservable<T>? EnsureStream<T>(IObservable<T>? s) => s ?? Observable.Empty<T>();

Try / catch

try { _stream.Subscribe(OnNext); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* log: stream not initialized */ }

Prevention

When it happens

Trigger: Calling someObservable.Subscribe(x => ...) where someObservable is a null IObservable<T> reference — e.g. a property/field never initialized, a factory method that returned null, or a service-injected stream not registered in the container.

Common situations: Subscribing to an event stream from an injected service that wasn't registered; a method like GetObservable() returning null on some code path; refactoring where the observable field was removed but the subscription remained.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/7ef9086811203491. Report an issue: GitHub.

Appendix: source

Thrown at src/Avalonia/Prism.Avalonia/Extensions/ObservableExtensions.cs:21

using Prism.Common;

namespace Prism.Extensions
{
    internal static class ObservableExtensions
    {
        /// <summary>
        /// Subscribes an element handler to an observable sequence.
        /// </summary>
        /// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Observable sequence to subscribe to.</param>
        /// <param name="onNext">Action to invoke for each element in the observable sequence.</param>
        /// <returns><see cref="IDisposable"/> object used to unsubscribe from the observable sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is <c>null</c>.</exception>
        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (onNext == null)
            {
                throw new ArgumentNullException(nameof(onNext));
            }

            //
            // [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally.
            //
            return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(onNext, Stubs.Throw, Stubs.Nop));
        }
    }
}

View on GitHub (pinned to 358118cd64)