{"record":{"id":"92f8166a94d15291","repo":"dotnet/reactive","slug":"argumentnullexception-timestamp","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timestamp.cs","lineNumber":14,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// 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;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<Timestamped<TSource>> Timestamp<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, Timestamped<TSource>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Timestamp(observer)));\n        }\n\n        public static IAsyncObservable<Timestamped<TSource>> Timestamp<TSource>(this IAsyncObservable<TSource> source, IClock clock)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (clock == null)\n                throw new ArgumentNullException(nameof(clock));\n\n            return CreateAsyncObservable<Timestamped<TSource>>.From(\n                source,\n                clock,\n                static (source, clock, observer) => source.SubscribeSafeAsync(AsyncObserver.Timestamp(observer, clock)));\n        }\n    }\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timestamp.cs#L1-L32","documentation":"The extension method AsyncObservable.Timestamp(source) throws ArgumentNullException when the source IAsyncObservable<TSource> is null. Rx-style operators validate the source at the operator call so subscription-time failures point to the actual problem instead of a NullReferenceException deep inside SubscribeSafeAsync.","triggerScenarios":"Chaining .Timestamp() on an observable variable that is null, e.g. a method returned null instead of an empty observable, or a conditional pipeline assembled the source lazily.","commonSituations":"Factory methods that return null on failure instead of AsyncObservable.Empty<T>(); nullable fields captured before initialization; switching branches where one branch forgets to assign the observable.","solutions":["Ensure the upstream observable is non-null before calling Timestamp; use AsyncObservable.Empty<TSource>() instead of null for 'no events'.","Add a null check (ArgumentNullException.ThrowIfNull(source)) at the boundary that produces the observable.","Fix the producing method to never return null; return an empty or failed observable."],"exampleFix":"// before\nvar src = pipeline.Build(); // may return null\nvar ts = src.Timestamp();\n\n// after\nvar src = pipeline.Build() ?? AsyncObservable.Empty<MyEvent>();\nvar ts = src.Timestamp();","handlingStrategy":"validation","validationCode":"if (source is null)\n    throw new InvalidOperationException(\"Timestamp requires a non-null source observable.\");","typeGuard":"bool HasSource<T>(IAsyncObservable<T>? source) => source is not null;","tryCatchPattern":"try\n{\n    return source.Timestamp();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    return AsyncObservable.Empty<Timestamped<T>>().Timestamp();\n}","preventionTips":["Never return null observables from factories; return AsyncObservable.Empty<T>().","Coalesce null sources at pipeline boundaries with ?? AsyncObservable.Empty<T>().","Enable nullable reference types so null flow is caught at compile time."],"tags":["csharp","asyncrx","argument-validation","null-check","operators"],"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"}