{"record":{"id":"6c8dff2bd347230c","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-sources-case","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'sources')","messagePattern":"Value cannot be null\\. \\(Parameter 'sources'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Case.cs","lineNumber":22,"sourceCode":"\nusing System.Collections.Generic;\nusing System.Reactive.Concurrency;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IAsyncObservable<TResult>> sources) => Case(selector, sources, Empty<TResult>());\n\n        public static IAsyncObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IAsyncObservable<TResult>> sources, IAsyncScheduler scheduler) => Case(selector, sources, Empty<TResult>(scheduler));\n\n        public static IAsyncObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IAsyncObservable<TResult>> sources, IAsyncObservable<TResult> defaultSource)\n        {\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n            if (sources == null)\n                throw new ArgumentNullException(nameof(sources));\n            if (defaultSource == null)\n                throw new ArgumentNullException(nameof(defaultSource));\n\n            return Create<TResult>(observer =>\n            {\n                var source = default(IAsyncObservable<TResult>);\n\n                try\n                {\n                    var value = selector();\n\n                    if (!sources.TryGetValue(value, out source))\n                    {\n                        source = defaultSource;\n                    }\n                }\n                catch (Exception ex)\n                {","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Case.cs#L4-L40","documentation":"Thrown by Case<TValue, TResult>(selector, sources, defaultSource) when the sources dictionary is null. Case looks up the selector's value in this IDictionary to choose the source observable; without the dictionary there is nothing to dispatch to, so the operator rejects null eagerly.","triggerScenarios":"Calling Case(selector, sources, defaultSource) with a null IDictionary<TValue, IAsyncObservable<TResult>>, often a dictionary built lazily or from a config registry that failed to load.","commonSituations":"Source registry populated at startup but accessed before initialization; DI not providing the mapping; refactoring renamed the dictionary field leaving it null.","solutions":["Build and pass a non-empty sources dictionary (keys matching selector outputs).","Ensure the registry/dictionary is initialized before composing the pipeline.","Use the dictionary-preserving overload Case(selector, sources, scheduler) if a default isn't needed, but still pass a non-null map."],"exampleFix":"// before\nvar stream = Case(selector, sourceMap, empty); // sourceMap null before init\n// after\nvar map = sourceMap ?? new Dictionary<Key, IAsyncObservable<Result>>();\nvar stream = Case(selector, map, empty);","handlingStrategy":"validation","validationCode":"if (sources is null) throw new ArgumentNullException(nameof(sources));","typeGuard":"bool HasSources<TValue, TResult>(IDictionary<TValue, IAsyncObservable<TResult>>? d) => d is { Count: > 0 };","tryCatchPattern":"try { var s = Case(selector, sources, defaultSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"sources\") { /* initialize registry */ }","preventionTips":["Populate the sources dictionary before building pipelines that use it","Fail fast at startup if the source registry is empty or null","Use Lazy<T>/initialization guards for lazily built registries"],"tags":["asyncrx","argumentnull","case","sources"],"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"}