{"record":{"id":"5fc64f3c8732559c","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-lastordefault","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/LastOrDefault.cs","lineNumber":57,"sourceCode":"        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LastOrDefault(observer, predicate)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> LastOrDefault<TSource>(IAsyncObserver<TSource> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            var hasValue = false;\n            var lastValue = default(TSource);\n\n            return Create<TSource>(\n                x =>\n                {\n                    hasValue = true;\n                    lastValue = x;\n\n                    return default;\n                },\n                observer.OnErrorAsync,\n                async () =>\n                {\n                    await observer.OnNextAsync(hasValue ? lastValue : default).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                }","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/LastOrDefault.cs#L39-L75","documentation":"ArgumentNullException thrown by the observer-level factory AsyncObserver.LastOrDefault<TSource>(IAsyncObserver<TSource>) when the downstream observer is null. This factory builds the operator's observation logic; it is normally invoked via LastOrDefaultAsync with a valid observer, so hitting it means null was passed directly into the observer factory.","triggerScenarios":"Calling AsyncObserver.LastOrDefault<T>(null), or building a custom subscription pipeline that forwards a null downstream observer.","commonSituations":"Custom operator implementations whose observer parameter was not validated upstream, test harnesses constructing observers manually, or composition order mistakes.","solutions":["Pass a valid IAsyncObserver<T> instance to the factory","Validate the observer in the surrounding custom operator before delegating","Use the public LastOrDefault operator instead of the observer factory directly","Check the observer-producing call upstream for null returns"],"exampleFix":"// before\nvar obs = AsyncObserver.LastOrDefault(downstream);\n// after\nif (downstream == null) throw new ArgumentNullException(nameof(downstream));\nvar obs = AsyncObserver.LastOrDefault(downstream);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));","typeGuard":"static bool IsValidObserver<T>(IAsyncObserver<T>? o) => o is not null;","tryCatchPattern":"try\n{\n    var obs = AsyncObserver.LastOrDefault(downstream);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\")\n{\n    // downstream observer was not wired; fix composition\n}","preventionTips":["Always validate observer parameters in custom operator code before delegating to factories","Prefer the public operator API over observer factories unless writing custom operators","Wire observers in a fixed order so downstream is never null"],"tags":["argumentnull","null-observer","observer-factory","async-rx"],"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"}