{"record":{"id":"969192f7833028d7","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-newcollector","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'newCollector')","messagePattern":"Value cannot be null\\. \\(Parameter 'newCollector'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs","lineNumber":54,"sourceCode":"        /// Produces an enumerable sequence that returns elements collected/aggregated from the source sequence between consecutive iterations.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements produced by the merge operation during collection.</typeparam>\n        /// <param name=\"source\">Source observable sequence.</param>\n        /// <param name=\"newCollector\">Factory to create a new collector object.</param>\n        /// <param name=\"merge\">Merges a sequence element with the current collector.</param>\n        /// <returns>The enumerable sequence that returns collected/aggregated elements from the source sequence upon each iteration.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"newCollector\"/> or <paramref name=\"merge\"/> is null.</exception>\n        public static IEnumerable<TResult> Collect<TSource, TResult>(this IObservable<TSource> source, Func<TResult> newCollector, Func<TResult, TSource, TResult> merge)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (newCollector == null)\n            {\n                throw new ArgumentNullException(nameof(newCollector));\n            }\n\n            if (merge == null)\n            {\n                throw new ArgumentNullException(nameof(merge));\n            }\n\n            return s_impl.Collect(source, newCollector, merge);\n        }\n\n        /// <summary>\n        /// Produces an enumerable sequence that returns elements collected/aggregated from the source sequence between consecutive iterations.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements produced by the merge operation during collection.</typeparam>\n        /// <param name=\"source\">Source observable sequence.</param>\n        /// <param name=\"getInitialCollector\">Factory to create the initial collector object.</param>\n        /// <param name=\"merge\">Merges a sequence element with the current collector.</param>","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs#L36-L72","documentation":"System.ArgumentNullException thrown by Observable.Collect(source, newCollector, merge) when the newCollector factory delegate is null. Rx validates each argument in order (source, newCollector, merge) so this fires only when source is non-null but the collector factory is not supplied.","triggerScenarios":"Calling Collect with a null second argument, e.g. Collect(source, null, (acc, x) => ...) — typically when the delegate is built dynamically, retrieved from config/reflection, or a lambda was accidentally omitted.","commonSituations":"Passing a method group that resolves to null (e.g. a nullable Func field), constructing the call generically where the collector seed is optional, refactoring that renamed/removed the seed factory.","solutions":["Supply a valid Func<TResult> factory, e.g. () => default or () => new Accumulator().","If the seed may be absent, guard with a null-coalescing default delegate before calling.","Check that any delegate variable/field is initialized before being passed in."],"exampleFix":"// before\nvar result = source.Collect(collectorFactory, (acc, x) => acc + x); // collectorFactory == null\n// after\nvar result = source.Collect(() => 0, (acc, x) => acc + x);","handlingStrategy":"validation","validationCode":"if (newCollector is null) throw new InvalidOperationException(\"Collect requires a non-null collector factory\");\nvar safeFactory = newCollector ?? (() => default(TResult));","typeGuard":"bool HasFactory<TResult>(Func<TResult>? f) => f is not null;","tryCatchPattern":"try\n{\n    var result = source.Collect(newCollector, merge);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"newCollector\")\n{\n    // fall back to a default seed factory\n}","preventionTips":["Always pass an explicit lambda for the seed factory rather than a nullable delegate variable.","Keep delegate arguments inline where possible to avoid null indirection.","Use nullable reference type annotations on Func fields to surface uninitialized delegates."],"tags":["null-argument","rx","delegate"],"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"}