{"record":{"id":"5aacc0a471e0f867","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-defaultifempty","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/DefaultIfEmpty.cs","lineNumber":34,"sourceCode":"\n        public static IAsyncObservable<TSource> DefaultIfEmpty<TSource>(this IAsyncObservable<TSource> source, TSource defaultValue)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(\n                source,\n                defaultValue,\n                static (source, defaultValue, observer) => source.SubscribeSafeAsync(AsyncObserver.DefaultIfEmpty(observer, defaultValue)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> DefaultIfEmpty<TSource>(IAsyncObserver<TSource> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return DefaultIfEmpty(observer, default);\n        }\n\n        public static IAsyncObserver<TSource> DefaultIfEmpty<TSource>(IAsyncObserver<TSource> observer, TSource defaultValue)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            var hasValue = false;\n\n            return Create<TSource>(\n                x =>\n                {\n                    hasValue = true;\n                    return observer.OnNextAsync(x);\n                },\n                observer.OnErrorAsync,","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/DefaultIfEmpty.cs#L16-L52","documentation":"AsyncObserver.DefaultIfEmpty requires an upstream IAsyncObserver<TSource> to wrap. The library eagerly validates arguments and throws ArgumentNullException naming 'observer' when null is passed, because a null observer cannot receive OnNext/OnError/OnCompleted notifications.","triggerScenarios":"Calling AsyncObserver.DefaultIfEmpty<TSource>(null) or DefaultIfEmpty<TSource>(null, defaultValue) — i.e. the first argument is a null IAsyncObserver<TSource>, typically because an upstream observer variable was never assigned or a factory returned null.","commonSituations":"Composing operator pipelines where an intermediate observer comes from a nullable factory result or dictionary lookup; refactoring that renamed an observer field leaving it uninitialized; passing the result of a conditional expression that falls through to null.","solutions":["Pass a non-null IAsyncObserver<TSource> as the first argument to DefaultIfEmpty.","Null-check or use the null-coalescing operator on the observer variable before calling DefaultIfEmpty.","Fix the upstream factory/lookup that produced the null observer instead of propagating it."],"exampleFix":"// before\nvar obs = GetUpstreamObserver(); // may return null\nvar withDefault = AsyncObserver.DefaultIfEmpty(obs, 42);\n// after\nvar obs = GetUpstreamObserver() ?? throw new InvalidOperationException(\"no upstream observer\");\nvar withDefault = AsyncObserver.DefaultIfEmpty(obs, 42);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nvar op = AsyncObserver.DefaultIfEmpty(observer, default);","typeGuard":"static bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;","tryCatchPattern":"try\n{\n    var op = AsyncObserver.DefaultIfEmpty(observer);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\")\n{\n    // observer argument was null; fix upstream observer construction\n}","preventionTips":["Never let observer variables remain uninitialized; initialize at declaration or use '?? throw'.","Wrap operator composition in helper methods that validate inputs once.","Enable nullable reference types (C# 8+) so null flows are caught at compile time."],"tags":["null-argument","rx","async","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"}