{"record":{"id":"419352d9dd551cf3","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-selector-419352","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'selector')","messagePattern":"Value cannot be null\\. \\(Parameter 'selector'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Publish.cs","lineNumber":57,"sourceCode":"\n            return new PublishedBuffer<TSource>(source.GetEnumerator());\n        }\n\n        /// <summary>\n        /// Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the\n        /// source sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"selector\">Selector function with published access to the source sequence for each enumerator.</param>\n        /// <returns>Sequence resulting from applying the selector function to the published view over the source sequence.</returns>\n        public static IEnumerable<TResult> Publish<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return Create(() => selector(source.Publish()).GetEnumerator());\n        }\n\n        private sealed class PublishedBuffer<T> : IBuffer<T>\n        {\n            private readonly object _gate = new();\n            private readonly RefCountList<T> _buffer;\n            private readonly IEnumerator<T> _source;\n\n            private bool _disposed;\n            private Exception? _error;\n            private bool _stopped;\n\n            public PublishedBuffer(IEnumerator<T> source)\n            {\n                _buffer = new RefCountList<T>(0);\n                _source = source;","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Publish.cs#L39-L75","documentation":"Publish(source, selector) throws ArgumentNullException when the selector function is null. The selector is invoked lazily inside the query, so the library rejects a null selector up front to fail fast at the call site.","triggerScenarios":"Calling source.Publish(null) or passing a delegate variable that was never assigned.","commonSituations":"Storing the selector in a field/property or receiving it as an optional parameter that arrived null; conditional lambda construction that fell through to null.","solutions":["Pass a valid Func<IEnumerable<TSource>, IEnumerable<TResult>>; if no projection is needed, use the parameterless Publish(source) overload.","Coalesce to an identity selector: selector ?? (Func<IEnumerable<int>, IEnumerable<int>>)(s => s).","Fix the code path that left the delegate null."],"exampleFix":"// before\nvar result = source.Publish(selector); // selector may be null\n// after\nvar result = source.Publish(selector ?? (s => s));","handlingStrategy":"validation","validationCode":"if (selector == null) selector = s => s; // identity\nvar result = source.Publish(selector);","typeGuard":"static bool HasSelector<TSource, TResult>(Func<IEnumerable<TSource>, IEnumerable<TResult>>? selector) => selector is not null;","tryCatchPattern":"try\n{\n    var result = source.Publish(selector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"selector\")\n{\n    // handle missing selector\n}","preventionTips":["Never pass possibly-null delegate variables into Publish; assign a lambda inline.","Coalesce with an identity selector when projection is optional.","Prefer method-group or lambda literals over nullable delegate fields."],"tags":["null-reference","argument-validation","linq"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}