{"record":{"id":"54e0b1f72021e67e","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-scheduler-observable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'scheduler')","messagePattern":"Value cannot be null\\. \\(Parameter 'scheduler'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs","lineNumber":1677,"sourceCode":"        /// <param name=\"scheduler\">Scheduler used to produce an OnCompleted message in case <paramref name=\"count\">count</paramref> is set to 0.</param>\n        /// <returns>An observable sequence that contains the specified number of elements from the start of the input sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"count\"/> is less than zero.</exception>\n        public static IObservable<TSource> Take<TSource>(this IObservable<TSource> source, int count, IScheduler scheduler)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (count < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count));\n            }\n\n            if (scheduler == null)\n            {\n                throw new ArgumentNullException(nameof(scheduler));\n            }\n\n            return s_impl.Take(source, count, scheduler);\n        }\n\n        #endregion\n\n        #region + TakeWhile +\n\n        /// <summary>\n        /// Returns elements from an observable sequence as long as a specified condition is true.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">A sequence to return elements from.</param>\n        /// <param name=\"predicate\">A function to test each element for a condition.</param>\n        /// <returns>An observable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"predicate\"/> is null.</exception>\n        public static IObservable<TSource> TakeWhile<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)","sourceCodeStart":1659,"sourceCodeEnd":1695,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs#L1659-L1695","documentation":"System.Reactive's public Take(source, count, scheduler) overload validates its arguments eagerly and synchronously. When the optional-behavior overload is called with a null IScheduler, it throws ArgumentNullException so scheduling configuration errors fail fast at composition time rather than inside the subscription pipeline. Rx operators intentionally never accept null arguments for scheduler/source/selector parameters.","triggerScenarios":"Calling Observable.Take(source, count, scheduler) with scheduler == null, e.g. Take(source, 5, (IScheduler)null), typically from a variable or DI-resolved scheduler that was never assigned.","commonSituations":"A Scheduler field/property defaulted to null instead of Scheduler.Default; a null returned from a config or factory lookup for the test scheduler; refactoring from the 2-arg Take overload to the timed Take(source, count, scheduler) overload while passing a placeholder null.","solutions":["Pass a concrete scheduler such as Scheduler.Default, Scheduler.CurrentThread, or Scheduler.Immediate.","If the scheduler comes from DI or config, default it with `scheduler ?? Scheduler.Default` at the call site.","If you do not need custom scheduling, call the Take(source, count) overload that needs no scheduler."],"exampleFix":"// before\nvar result = source.Take(5, myScheduler); // myScheduler is null\n\n// after\nvar result = source.Take(5, myScheduler ?? Scheduler.Default);","handlingStrategy":"validation","validationCode":"if (scheduler is null) scheduler = Scheduler.Default; // before calling Take(source, count, scheduler)\nArgumentNullException.ThrowIfNull(source);","typeGuard":"bool HasScheduler(IScheduler? s) => s is not null;","tryCatchPattern":"try { var r = source.Take(count, scheduler); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\") { /* fall back to Scheduler.Default */ }","preventionTips":["Initialize scheduler fields to Scheduler.Default, never null.","Prefer the scheduler-less Take(source, count) overload unless timing control is needed.","Enable nullable reference types so unassigned IScheduler fields are compile-time errors."],"tags":["rx","argument-null","scheduler","dotnet"],"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"}