{"record":{"id":"6a8157bbb6cf32f5","repo":"dotnet/reactive","slug":"argumentnullexception-timer","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timer.cs","lineNumber":20,"sourceCode":"// 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.Concurrency;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<long> Timer(TimeSpan dueTime)\n        {\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime));\n        }\n\n        public static IAsyncObservable<long> Timer(TimeSpan dueTime, IAsyncScheduler scheduler)\n        {\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime, scheduler));\n        }\n\n        public static IAsyncObservable<long> Timer(DateTimeOffset dueTime)\n        {\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime));\n        }\n\n        public static IAsyncObservable<long> Timer(DateTimeOffset dueTime, IAsyncScheduler scheduler)\n        {\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime, scheduler));\n        }\n\n        public static IAsyncObservable<long> Timer(TimeSpan dueTime, TimeSpan period)","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timer.cs#L2-L38","documentation":"The Timer(TimeSpan, IAsyncScheduler) factory throws ArgumentNullException because the 'scheduler' parameter was null. Timer needs a scheduler to schedule the single emission at dueTime; the library validates this before creating the observable.","triggerScenarios":"Calling AsyncObservable.Timer(dueTime, null) — e.g. a scheduler resolved from configuration or DI that is null, or a variable shadowing the intended scheduler.","commonSituations":"DI container failing to register an IAsyncScheduler implementation so the injected value is null; passing TimeSpan in the scheduler slot by mistake (overload argument-order mix-up).","solutions":["Pass TaskPoolAsyncScheduler.Default as the scheduler.","If the scheduler comes from DI/configuration, verify registration and null-check it before calling Timer.","Use the parameterless-scheduler overload Timer(TimeSpan dueTime), which defaults the scheduler internally."],"exampleFix":"// before\nvar timer = AsyncObservable.Timer(TimeSpan.FromSeconds(1), scheduler); // scheduler null from DI\n// after\nvar timer = AsyncObservable.Timer(TimeSpan.FromSeconds(1), scheduler ?? TaskPoolAsyncScheduler.Default);","handlingStrategy":"validation","validationCode":"if (scheduler is null) throw new ArgumentNullException(nameof(scheduler));\nvar timer = AsyncObservable.Timer(dueTime, scheduler);","typeGuard":"bool HasScheduler(IAsyncScheduler s) => s is not null;","tryCatchPattern":"try\n{\n    var timer = AsyncObservable.Timer(dueTime, scheduler);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\")\n{\n    var timer = AsyncObservable.Timer(dueTime); // default scheduler\n}","preventionTips":["Check DI/configuration for the IAsyncScheduler binding before use.","Use Timer(dueTime) without a scheduler when default behavior suffices.","Watch argument order: dueTime first, scheduler second."],"tags":["argument-validation","null-reference","async-rx","timer-operator","scheduler"],"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"}