{"record":{"id":"a3a39f3a0dee423a","repo":"AvaloniaUI/Avalonia","slug":"value-cannot-be-null-parameter-observer","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":"src/Avalonia.Base/Reactive/LightweightObservableBase.cs","lineNumber":26,"sourceCode":"    /// <summary>\n    /// Lightweight base class for observable implementations.\n    /// </summary>\n    /// <typeparam name=\"T\">The observable type.</typeparam>\n    /// <remarks>\n    /// ObservableBase{T} is rather heavyweight in terms of allocations and memory\n    /// usage. This class provides a more lightweight base for some internal observable types\n    /// in the Avalonia framework.\n    /// </remarks>\n    internal abstract class LightweightObservableBase<T> : IObservable<T>\n    {\n        private Exception? _error;\n        private List<IObserver<T>>? _observers = new List<IObserver<T>>();\n\n        public bool HasObservers => _observers?.Count > 0;\n\n        public IDisposable Subscribe(IObserver<T> observer)\n        {\n            _ = observer ?? throw new ArgumentNullException(nameof(observer));\n\n            //Dispatcher.UIThread.VerifyAccess();\n\n            var first = false;\n\n            for (; ; )\n            {\n                if (Volatile.Read(ref _observers) == null)\n                {\n                    if (_error != null)\n                    {\n                        observer.OnError(_error);\n                    }\n                    else\n                    {\n                        observer.OnCompleted();\n                    }\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Reactive/LightweightObservableBase.cs#L8-L44","documentation":"Thrown by LightweightObservableBase<T>.Subscribe when the IObserver<T> argument is null. LightweightObservableBase is an internal base for low-overhead observables that live on the UI thread. Subscribe stores the observer in a volatile list, so a null observer would NRE later during dispatch; the guard fails fast.","triggerScenarios":"Calling observable.Subscribe(null) directly, or passing an observer variable that resolved to null. Also reachable through binding/style machinery that wires up a null observer when a property source is unavailable.","commonSituations":"A reactive binding source returns null under a transient state; a custom observer field is not yet constructed when the subscription is made; subscribing via an overload that defaulted an observer to null.","solutions":["Pass a concrete observer instance (e.g. AnonymousObserver or a lambda via Subscribe(onNext, onError, onCompleted)).","Null-check the observer before subscribing.","If the source can legitimately be null, skip the subscription or substitute an no-op observer."],"exampleFix":"// before\nobs.Subscribe(observer); // observer is null\n// after\nobs.Subscribe(observer ?? new AnonymousObserver<T>(_ => { }));","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nobs.Subscribe(observer);","typeGuard":"static bool IsValidObserver<T>(IObserver<T>? o) => o is not null;","tryCatchPattern":null,"preventionTips":["Prefer Subscribe(onNext, onError?, onCompleted?) over passing a raw observer.","Construct observers before initiating subscriptions.","Null-check observers from binding adapters before forwarding."],"tags":["reactive","null-argument","observer","ui-thread"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}