dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

RepeatWhen wraps the source with a handler that receives completion signals; its Subscribe first validates the observer and throws ArgumentNullException("observer") when null. The library cannot route OnNext/OnError/OnCompleted without a valid observer.

Solutions

  1. Ensure a valid IObserver<T> is constructed before calling Subscribe.
  2. Use the Action-based Subscribe overloads or Observer.Create<T> to build the observer.
  3. Null-check the observer before subscribing.

Example fix

// before
source.RepeatWhen(h => h).Subscribe(null);
// after
source.RepeatWhen(h => h).Subscribe(Observer.Create<T>(onNext, onError, onCompleted));
Defensive patterns

Strategy: validation

Validate before calling

// C#
if (observer is null) throw new ArgumentNullException(nameof(observer));
var sub = source.RepeatWhen(c => c).Subscribe(observer);

Type guard

// C#
static bool IsValidObserver<T>(IObserver<T> o) => o is not null;

Try / catch

try { source.RepeatWhen(c => c).Subscribe(observer); }
catch (ArgumentNullException ex) { Log(ex); }

Prevention

When it happens

Trigger: Calling source.RepeatWhen(handler).Subscribe(null), or a null observer reaching Never-style argument validation inside RepeatWhenObservable.Subscribe.

Common situations: Observers obtained from a factory that returned null; uninitialized fields in custom subscription wrappers; refactored code dropping observer creation.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/RepeatWhen.cs:26

namespace System.Reactive.Linq.ObservableImpl
{
    internal sealed class RepeatWhen<T, U> : IObservable<T>
    {
        private readonly IObservable<T> _source;
        private readonly Func<IObservable<object>, IObservable<U>> _handler;

        internal RepeatWhen(IObservable<T> source, Func<IObservable<object>, IObservable<U>> handler)
        {
            _source = source;
            _handler = handler;
        }

        public IDisposable Subscribe(IObserver<T> observer)
        {
            if (observer == null)
            {
                throw new ArgumentNullException(nameof(observer));
            }

            var completeSignals = new Subject<object>();

            IObservable<U> redo;

            try
            {
                redo = _handler(completeSignals);
                
                if (redo == null)
                {
#pragma warning disable CA2201 // (Do not raise reserved exception types.) Backwards compatibility prevents us from complying.
                    throw new NullReferenceException("The handler returned a null IObservable");
#pragma warning restore CA2201
                }
            }
            catch (Exception ex)

View on GitHub (pinned to 94b5d5ab91)