{"record":{"id":"cc2380a17d0fe10d","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-contains","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/Contains.cs","lineNumber":41,"sourceCode":"        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return CreateAsyncObservable<bool>.From(\n                source,\n                (element, comparer),\n                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Contains(observer, state.element, state.comparer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Contains<TSource>(IAsyncObserver<bool> observer, TSource element)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Contains(observer, element, EqualityComparer<TSource>.Default);\n        }\n\n        public static IAsyncObserver<TSource> Contains<TSource>(IAsyncObserver<bool> observer, TSource element, IEqualityComparer<TSource> comparer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return Create<TSource>(\n                async x =>\n                {\n                    var equals = false;\n                    try\n                    {\n                        equals = comparer.Equals(x, element);","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Contains.cs#L23-L59","documentation":"AsyncObserver.Contains(observer, element) throws ArgumentNullException because the downstream bool observer is null. This library validates all observer arguments eagerly at operator construction so subscription failures surface at the call site rather than later inside the async pipeline. Passing null here means the Contains operator would have nowhere to emit its result.","triggerScenarios":"Calling AsyncObserver.Contains<TSource>(null, someElement) directly, or composing custom operator pipelines where a downstream observer variable was never initialized before being threaded into Contains.","commonSituations":"Hand-rolled operator composition in custom async Rx pipelines; a factory method returning null observer; refactoring that removed observer initialization; wiring observers conditionally and hitting a path where none was assigned.","solutions":["Pass a non-null IAsyncObserver<bool> (e.g. the observer received in your Subscribe delegate) to AsyncObserver.Contains.","If you have no downstream observer, create one via AsyncObserver.Create or subscribe through AsyncObservable.Contains instead of calling the observer factory directly.","Check any intermediate factory method that supplies the observer for accidental null returns."],"exampleFix":"// before\nvar op = AsyncObserver.Contains<int>(null, 42);\n// after\nawait AsyncObservable.Contains(source, 42).ForEachAsync(x => Console.WriteLine(x));","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 { var op = AsyncObserver.Contains(observer, element); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* supply a valid observer or abort composition */ }","preventionTips":["Never construct AsyncObserver.* factories manually unless writing custom operators; prefer the AsyncObservable extensions.","Null-check observer parameters at the top of custom operators.","Enable nullable reference types (C# nullable annotations) so null observer arguments are flagged at compile time."],"tags":["argument-null","async-rx","operator"],"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"}