dotnet/reactive · error · ArgumentNullException

subject

Error message

subject

What it means

The two-parameter Multicast(source, subject) overload throws ArgumentNullException with param name "subject" when the ISubject<TSource, TResult> is null. The subject is the multicast hub that receives the source's notifications and republishes them; without it the operator cannot work, so the library validates it immediately. This is the second guard in the same method after the source check.

Solutions

  1. Create the subject before calling Multicast (e.g. new Subject<T>() or AsyncSubject<T>() as appropriate).
  2. If you do not need a custom subject, use Publish (Multicast with a Subject) instead.
  3. Guard the subject creation so it can never be null at the call site.

Example fix

// before
var connectable = source.Multicast(mySubject); // mySubject is null
// after
var connectable = source.Multicast(new Subject<int>());
Defensive patterns

Strategy: validation

Validate before calling

if (subject is null) throw new ArgumentNullException(nameof(subject));
var connectable = source.Multicast(subject);

Type guard

static bool HasSubject<TSource, TResult>(ISubject<TSource, TResult>? s) => s is not null;

Try / catch

try { connectable = source.Multicast(subject); }
catch (ArgumentNullException ex) when (ex.ParamName == "subject") { connectable = source.Multicast(new Subject<TSource>()); }

Prevention

When it happens

Trigger: Calling source.Multicast(subject) where the subject argument is null — e.g. Subject.Create(observer, observable) built from null parts, a lazily-created subject field, or passing a custom ISubject implementation that a factory returned as null.

Common situations: Building custom hot-stream plumbing where a subject is created conditionally; DI containers that fail to resolve ISubject and inject null; typos assigning the subject after the Multicast call.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs:34

        /// connectable observable, the subject is subscribed to the source exactly one, and messages are forwarded to the observers registered with
        /// the connectable observable. For specializations with fixed subject types, see Publish, PublishLast, and Replay.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be pushed into the specified subject.</param>
        /// <param name="subject">Subject to push source elements into.</param>
        /// <returns>A connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="subject"/> is null.</exception>
        public static IConnectableObservable<TResult> Multicast<TSource, TResult>(this IObservable<TSource> source, ISubject<TSource, TResult> subject)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return s_impl.Multicast(source, subject);
        }

        /// <summary>
        /// Multicasts the source sequence notifications through an instantiated subject into all uses of the sequence within a selector function. Each
        /// subscription to the resulting sequence causes a separate multicast invocation, exposing the sequence resulting from the selector function's
        /// invocation. For specializations with fixed subject types, see Publish, PublishLast, and Replay.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TIntermediate">The type of the elements produced by the intermediate subject.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
        /// <param name="source">Source sequence which will be multicasted in the specified selector function.</param>
        /// <param name="subjectSelector">Factory function to create an intermediate subject through which the source sequence's elements will be multicast to the selector function.</param>
        /// <param name="selector">Selector function which can use the multicasted source sequence subject to the policies enforced by the created subject.</param>
        /// <returns>An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="subjectSelector"/> or <paramref name="selector"/> is null.</exception>

View on GitHub (pinned to 94b5d5ab91)