{"record":{"id":"e99a89f616339494","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-subject","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'subject')","messagePattern":"Value cannot be null\\. \\(Parameter 'subject'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Multicast.cs","lineNumber":18,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Disposables;\nusing System.Reactive.Subjects;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IConnectableAsyncObservable<TResult> Multicast<TSource, TResult>(this IAsyncObservable<TSource> source, IAsyncSubject<TSource, TResult> subject)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (subject == null)\n                throw new ArgumentNullException(nameof(subject));\n\n            return new ConnectableAsyncObservable<TSource, TResult>(source, subject);\n        }\n\n        public static IAsyncObservable<TSource> Multicast<TSource>(this IAsyncObservable<TSource> source, Func<IAsyncSubject<TSource>> subjectFactory)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (subjectFactory == null)\n                throw new ArgumentNullException(nameof(subjectFactory));\n\n            return Multicast(source, () => new ValueTask<IAsyncSubject<TSource, TSource>>(subjectFactory()), x => new ValueTask<IAsyncObservable<TSource>>(x));\n        }\n\n        public static IAsyncObservable<TSource> Multicast<TSource>(this IAsyncObservable<TSource> source, Func<ValueTask<IAsyncSubject<TSource>>> subjectFactory)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Multicast.cs#L1-L36","documentation":"The subject parameter of Multicast is validated after the source. The IAsyncSubject<TSource, TResult> is the shared hub that multicasts source notifications to subscribers, so null is rejected immediately when the connectable is created.","triggerScenarios":"Calling source.Multicast(null), or calling Publish/PublishLast/Replay variants whose internal subject construction returns null and is forwarded here.","commonSituations":"A subject factory or DI-resolved subject that returned null; using the wrong null-able type when a custom IAsyncSubject implementation was intended.","solutions":["Pass a valid subject such as new AsyncSubject<TSource>() or Async BehaviorSubject","Fix the subject factory/DI registration that returns null","Use Publish/Replay helpers instead of hand-constructing subjects when possible"],"exampleFix":"// before\nsource.Multicast(null);\n// after\nsource.Multicast(new AsyncSubject<int>());","handlingStrategy":"validation","validationCode":"if (subject == null) throw new ArgumentNullException(nameof(subject));","typeGuard":"bool IsValidSubject<TSource,TResult>(IAsyncSubject<TSource,TResult> s) => s is not null;","tryCatchPattern":"try { var conn = source.Multicast(subject); } catch (ArgumentNullException ex) when (ex.ParamName == \"subject\") { /* create a subject */ }","preventionTips":["Prefer Publish/Replay helpers over manual subjects to avoid null subjects","Register subjects in DI with non-null defaults","Create subjects inline at the call site"],"tags":["asyncrx","argumentnull","multicast","subject"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}