{"record":{"id":"8e7a1f581851495a","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-materialize","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/Materialize.cs","lineNumber":23,"sourceCode":"namespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<Notification<TSource>> Materialize<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, Notification<TSource>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Materialize(observer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Materialize<TSource>(IAsyncObserver<Notification<TSource>> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Create<TSource>(\n                x => observer.OnNextAsync(Notification.CreateOnNext(x)),\n                async ex =>\n                {\n                    await observer.OnNextAsync(Notification.CreateOnError<TSource>(ex)).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                },\n                async () =>\n                {\n                    await observer.OnNextAsync(Notification.CreateOnCompleted<TSource>()).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                }\n            );\n        }\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Materialize.cs#L5-L41","documentation":"Materialize wraps an observer so that every OnNext/OnError/OnCompleted is surfaced as a Notification<TSource> value. The library eagerly validates that the downstream observer is not null and throws ArgumentNullException with parameter name 'observer' at the top of the factory method, before any subscription happens.","triggerScenarios":"Calling AsyncObserver.Materialize<TSource>(null), or passing a variable/chain expression that resolves to null (e.g. a factory method that returned null instead of an observer).","commonSituations":"Building custom operator pipelines where an observer is produced conditionally; refactoring code so a previously non-null observer field is no longer initialized; calling Materialize before wiring up the pipeline.","solutions":["Pass a non-null IAsyncObserver<Notification<TSource>> instance to Materialize","Check the factory/expression producing the observer for a code path that returns null","If the observer may legitimately be absent, use AsyncObserver./* no-op */ observer or a stub implementation instead of null"],"exampleFix":"// before\nvar obs = GetObserverOrNull();\nvar m = AsyncObserver.Materialize<int>(obs); // throws if null\n// after\nvar obs = GetObserverOrNull() ?? AsyncObserver.Create<Notification<int>>(async n => { });\nvar m = AsyncObserver.Materialize<int>(obs);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\n// or guard at call site:\nSystem.Diagnostics.Debug.Assert(observer != null, \"observer must be non-null before Materialize\");","typeGuard":"static bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;","tryCatchPattern":"try\n{\n    var materialized = AsyncObserver.Materialize(sourceObserver);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\")\n{\n    // supply a default observer or surface a configuration error\n}","preventionTips":["Never pass null where an IAsyncObserver is expected; use a no-op observer instead","Validate delegates/observers at pipeline construction time, not at subscribe time","Keep observer creation in one factory that never returns null"],"tags":["csharp","async-rx","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"}