{"record":{"id":"97f123ebb35cbf46","repo":"AvaloniaUI/Avalonia","slug":"value-cannot-be-null-parameter-dispose","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'dispose')","messagePattern":"Value cannot be null\\. \\(Parameter 'dispose'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Reactive/Disposable.cs","lineNumber":76,"sourceCode":"        }\n    }\n\n    /// <summary>\n    /// Gets the disposable that does nothing when disposed.\n    /// </summary>\n    public static IDisposable Empty => EmptyDisposable.Instance;\n\n    /// <summary>\n    /// Creates a disposable object that invokes the specified action when disposed.\n    /// </summary>\n    /// <param name=\"dispose\">Action to run during the first call to <see cref=\"IDisposable.Dispose\"/>. The action is guaranteed to be run at most once.</param>\n    /// <returns>The disposable object that runs the given action upon disposal.</returns>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispose\"/> is <c>null</c>.</exception>\n    public static IDisposable Create(Action dispose)\n    {\n        if (dispose == null)\n        {\n            throw new ArgumentNullException(nameof(dispose));\n        }\n\n        return new AnonymousDisposable(dispose);\n    }\n\n    /// <summary>\n    /// Creates a disposable object that invokes the specified action when disposed.\n    /// </summary>\n    /// <param name=\"state\">The state to be passed to the action.</param>\n    /// <param name=\"dispose\">Action to run during the first call to <see cref=\"IDisposable.Dispose\"/>. The action is guaranteed to be run at most once.</param>\n    /// <returns>The disposable object that runs the given action upon disposal.</returns>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispose\"/> is <c>null</c>.</exception>\n    public static IDisposable Create<TState>(TState state, Action<TState> dispose)\n    {\n        if (dispose == null)\n        {\n            throw new ArgumentNullException(nameof(dispose));\n        }","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Reactive/Disposable.cs#L58-L94","documentation":"Thrown by Disposable.Create(Action) when the dispose delegate passed in is null. Disposable is an internal Avalonia helper (mirroring System.Reactive's Disposable) that builds an AnonymousDisposable running the given action exactly once. The null guard exists because invoking a null delegate on disposal would otherwise throw a NullReferenceException later, far from the cause.","triggerScenarios":"Calling Disposable.Create(null) or Disposable.Create(someExpressionThatEvaluatesToNull). This is framework-internal code, typically reached via an Avalonia subsystem that wraps a cleanup callback (event unsubscription, handle release) in a disposable.","commonSituations":"A refactor removes the cleanup body and leaves a null literal; a conditional that yields a null Action under an edge branch is passed in; a third-party integration hands Avalonia a null teardown callback.","solutions":["Pass a non-null Action (even an empty lambda () => { }) to Create.","If the cleanup is conditional, build the Action before calling Create and default it to a no-op instead of null.","Trace the stack to the Avalonia subsystem that invoked Create and fix the null source there."],"exampleFix":"// before\nvar d = Disposable.Create(cleanup as Action); // cleanup may be null\n// after\nvar d = Disposable.Create(cleanup ?? (() => { }));","handlingStrategy":"validation","validationCode":"if (dispose is null) throw new ArgumentNullException(nameof(dispose));\nvar d = Disposable.Create(dispose);","typeGuard":"static bool IsValidDisposeAction(Action? a) => a is not null;","tryCatchPattern":null,"preventionTips":["Never pass a possibly-null Action to Disposable.Create; coalesce with a no-op lambda.","Treat Disposable as internal — prefer System.Reactive.Disposable.Create in app code.","Static-analyze for null literal arguments to Create."],"tags":["reactive","null-argument","disposable","internal-api"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}