{"record":{"id":"ae334733ecdbf646","repo":"dotnet/reactive","slug":"argumentnullexception-default-message-value-cannot-be-null-ae3347","errorCode":null,"errorMessage":"ArgumentNullException (default message: Value cannot be null. (Parameter 'source'))","messagePattern":"ArgumentNullException \\(default message: Value cannot be null\\. \\(Parameter 'source'\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs","lineNumber":28,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public static partial class Observable\n    {\n        #region + Cast +\n\n        /// <summary>\n        /// Converts the elements of an observable sequence to the specified type.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type to convert the elements in the source sequence to.</typeparam>\n        /// <param name=\"source\">The observable sequence that contains the elements to be converted.</param>\n        /// <returns>An observable sequence that contains each element of the source sequence converted to the specified type.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        public static IObservable<TResult> Cast<TResult>(this IObservable<object> source)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            return s_impl.Cast<TResult>(source);\n        }\n\n        #endregion\n\n        #region + DefaultIfEmpty +\n\n        /// <summary>\n        /// Returns the elements of the specified sequence or the type parameter's default value in a singleton sequence if the sequence is empty.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence (if any), whose default value will be taken if the sequence is empty.</typeparam>\n        /// <param name=\"source\">The sequence to return a default value for if it is empty.</param>\n        /// <returns>An observable sequence that contains the default value for the TSource type if the source is empty; otherwise, the elements of the source itself.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        public static IObservable<TSource?> DefaultIfEmpty<TSource>(this IObservable<TSource> source)\n        {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs#L10-L46","documentation":"System.Reactive's Cast operator requires a non-null source observable. The public API surface performs eager argument validation and throws ArgumentNullException before any subscription happens, matching Rx's documented contract that operator inputs must not be null.","triggerScenarios":"Calling Observable.Cast<TResult>(null) — i.e., passing a null IObservable<object> as the source argument.","commonSituations":"A field or factory method returning an observable is null (e.g., a service not yet initialized), or a nullable variable is passed without a null check before chaining LINQ-style Rx operators.","solutions":["Ensure the observable passed to Cast<TResult> is initialized before calling the operator","Add an explicit null check (ArgumentNullException.ThrowIfNull(source) or if (source == null)) before the call","If null means 'no stream', substitute Observable.Empty<object>() instead of null","Verify the code path that assigns the source actually ran (DI registration, async init)"],"exampleFix":"// before\nIObservable<object> src = _lazyStream?.Value; // may be null\nvar result = src.Cast<int>();\n// after\nvar result = (_lazyStream?.Value ?? Observable.Empty<object>()).Cast<int>();","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\n// or: ArgumentNullException.ThrowIfNull(source);","typeGuard":"bool HasSource(IObservable<object>? source) => source is not null;","tryCatchPattern":"try { var result = source.Cast<int>(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // use fallback stream or log\n    result = Observable.Empty<int>();\n}","preventionTips":["Never return null IObservable from factory methods; return Observable.Empty<T>()","Null-check observables at construction/boundary, not at every operator call","Prefer nullable reference types (IObservable<T>?) so the compiler flags risky passes","Use ArgumentNullException.ThrowIfNull early in your own pipeline builders"],"tags":["csharp","rx","argument-validation","null"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}