{"record":{"id":"5065714c987f53a9","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-range","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Range.cs","lineNumber":38,"sourceCode":"        public static IAsyncObservable<int> Range(int start, int count, IAsyncScheduler scheduler)\n        {\n            if (count < 0 || ((long)start) + count - 1 > int.MaxValue)\n                throw new ArgumentOutOfRangeException(nameof(count));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<int>(observer => AsyncObserver.Range(observer, start, count, scheduler));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static ValueTask<IAsyncDisposable> Range(IAsyncObserver<int> observer, int start, int count) => Range(observer, start, count, TaskPoolAsyncScheduler.Default);\n\n        public static ValueTask<IAsyncDisposable> Range(IAsyncObserver<int> observer, int start, int count, IAsyncScheduler scheduler)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (count < 0 || ((long)start) + count - 1 > int.MaxValue)\n                throw new ArgumentOutOfRangeException(nameof(count));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return scheduler.ScheduleAsync(async ct =>\n            {\n                if (ct.IsCancellationRequested)\n                    return;\n\n                for (int i = start, end = start + count - 1; i <= end && !ct.IsCancellationRequested; i++)\n                {\n                    await observer.OnNextAsync(i).RendezVous(scheduler, ct);\n                }\n\n                if (ct.IsCancellationRequested)\n                    return;\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Range.cs#L20-L56","documentation":"AsyncObserver.Range(observer, start, count) (and its scheduler overload) validates that the target IAsyncObserver<int> is non-null before scheduling value emission. A null observer throws ArgumentNullException naming 'observer'. The observer is the sink that receives the generated values, so the operation cannot proceed without it.","triggerScenarios":"Calling AsyncObserver.Range(null, 0, 10) directly; passing an observer variable that was never assigned, or the result of a factory/pipe that returned null; also triggered via the 4-argument overload with a null observer (the check runs before count/scheduler checks).","commonSituations":"Writing custom async observable operators that forward an observer parameter which is null; misusing the low-level AsyncObserver API instead of AsyncObservable.Range; test code constructing observers lazily and passing null on a failure path.","solutions":["Pass a valid IAsyncObserver<int> implementation (e.g. from CreateSubscribeAsync or an AsyncObserver helper).","If you only need the sequence, use AsyncObservable.Range(start, count) which wires the observer for you.","Check the code path that produced the observer reference; fix why it is null.","Add a non-null assertion/guard in your operator before delegating to AsyncObserver.Range."],"exampleFix":"// before\nawait AsyncObserver.Range(observer, 0, 10); // observer is null\n// after\nif (observer == null) throw new InvalidOperationException(\"observer not initialized\");\nawait AsyncObserver.Range(observer, 0, 10);","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentNullException(nameof(observer));","typeGuard":"static bool IsValidObserver(IAsyncObserver<int> observer) => observer is not null;","tryCatchPattern":"try { await AsyncObserver.Range(observer, start, count); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* initialize or substitute observer */ }","preventionTips":["Prefer AsyncObservable.Range over the low-level AsyncObserver API.","Assert observer non-null in your operator before delegating.","Avoid lazy initialization patterns that can leave the observer null.","Keep observer creation and subscription in the same code path."],"tags":["null-argument","observer","reactive","argument-validation"],"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"}