{"record":{"id":"856d73b2f98c305d","repo":"dotnet/reactive","slug":"nameof-source-observable-blocking","errorCode":null,"errorMessage":"nameof(source)","messagePattern":"nameof\\(source\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs","lineNumber":563,"sourceCode":"\n        #endregion\n\n        #region + Wait +\n\n        /// <summary>\n        /// Waits for the observable sequence to complete and returns the last element of the sequence.\n        /// If the sequence terminates with an OnError notification, the exception is thrown.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source observable sequence.</param>\n        /// <returns>The last element in the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"InvalidOperationException\">The source sequence is empty.</exception>\n        public static TSource Wait<TSource>(this IObservable<TSource> source)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            return s_impl.Wait(source);\n        }\n\n        #endregion\n    }\n}\n","sourceCodeStart":545,"sourceCodeEnd":572,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs#L545-L572","documentation":"The message is \"Value cannot be null. (Parameter 'source')\" thrown at Observable.Blocking.cs:563: Wait<TSource> validates that the IObservable<TSource> is non-null before blocking the calling thread until the sequence emits its single value. A null observable cannot be subscribed to, and Wait's contract (block and return the last element, throwing InvalidOperationException if empty) only makes sense on a real sequence.","triggerScenarios":"Calling Observable.Wait<TSource>(null) — e.g. waiting on a field/property holding the observable before it has been assigned, or on the result of an API that returned null instead of a sequence.","commonSituations":"Test code waiting on a subject/task-observable that failed to initialize; bridging synchronous code to Rx where the observable is produced by another component that returned null on failure; startup ordering bugs where Wait runs before the producer pipeline is built.","solutions":["Null-check before waiting: if (obs != null) { var v = obs.Wait(); }","Fix the producer to return Observable.Empty<TSource>() (Wait will then surface the documented empty-sequence InvalidOperationException) or a real sequence instead of null.","Ensure initialization ordering: the observable-producing component must be initialized before any Wait call."],"exampleFix":"// before\nvar result = _results.Wait(); // _results is null during startup\n// after\nif (_results == null)\n{\n    throw new InvalidOperationException(\"Results pipeline not initialized\");\n}\nvar result = _results.Wait();","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nvar result = source.Wait();","typeGuard":"bool CanWait<T>(IObservable<T>? source) => source is not null;","tryCatchPattern":"try\n{\n    var result = source.Wait();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // source was null; handle or rethrow with context\n    throw new InvalidOperationException(\"Result sequence not initialized\", ex);\n}","preventionTips":["Ensure the observable-producing component is initialized before any Wait call.","Remember Wait blocks the thread: on a valid sequence also be prepared for timeouts/empty-source InvalidOperationException.","Enable nullable reference types to catch uninitialized observable fields at compile time."],"tags":["rx","argument-null","blocking","wait"],"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"}