{"record":{"id":"d23290ec408b853c","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"window","errorCode":null,"errorMessage":"window","messagePattern":"window","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/DialogHostEx.cs","lineNumber":182,"sourceCode":"\n    /// <summary>\n    /// Shows a dialog using the parent/ancestor <see cref=\"DialogHost\"/> of the a given <see cref=\"DependencyObject\"/>.\n    /// </summary>\n    /// <param name=\"childDependencyObject\">Dependency object which should be a visual child of a <see cref=\"DialogHost\"/>, where the dialog will be shown.</param>\n    /// <param name=\"content\">Content to show (can be a control or view model).</param>\n    /// <param name=\"openedEventHandler\">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>\n    /// <param name=\"closingEventHandler\">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>\n    /// <param name=\"closedEventHandler\">Allows access to closed event which would otherwise have been subscribed to on a instance.</param>\n    /// <exception cref=\"InvalidOperationException\">\n    /// Thrown is a <see cref=\"DialogHost\"/> is not found when conducting a depth first traversal of visual tree.  \n    /// </exception>\n    /// <returns></returns>\n    public static Task<object?> ShowDialog(this DependencyObject childDependencyObject, object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler, DialogClosedEventHandler closedEventHandler)\n        => GetOwningDialogHost(childDependencyObject).ShowInternal(content, openedEventHandler, closingEventHandler, closedEventHandler);\n\n    private static DialogHost GetFirstDialogHost(Window window)\n    {\n        if (window is null) throw new ArgumentNullException(nameof(window));\n\n        DialogHost? dialogHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().FirstOrDefault();\n\n        if (dialogHost is null)\n            throw new InvalidOperationException(\"Unable to find a DialogHost in visual tree\");\n\n        return dialogHost;\n    }\n\n    private static DialogHost GetFirstDialogHost(Window window, object? dialogIdentifier)\n    {\n        if (window is null) throw new ArgumentNullException(nameof(window));\n\n        DialogHost? dialogHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().FirstOrDefault(x => x.Identifier is not null && x.Identifier.Equals(dialogIdentifier));\n\n        if (dialogHost is null)\n            throw new InvalidOperationException($\"Unable to find a DialogHost with identifier '{dialogIdentifier}' in visual tree\");\n","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/DialogHostEx.cs#L164-L200","documentation":"ArgumentNullException(nameof(window)) in GetFirstDialogHost(Window) (DialogHostEx.cs:182). Triggered when a ShowDialog extension method on Window is called with a null Window reference, before any visual-tree traversal runs.","triggerScenarios":"Calling window.ShowDialog(content) where 'window' is null (e.g. Window.GetWindow(this) returned null because the control is not yet in a loaded Window, or a field was never assigned).","commonSituations":"Calling ShowDialog from a control before it is loaded into a Window; using Application.Current.MainWindow before it is set; refactor leaving a Window field null.","solutions":["Null-check the Window before calling ShowDialog and surface a clearer error or skip.","Acquire the window via Window.GetWindow(this) only after the control is loaded (subscribe to Loaded).","Prefer DialogHost.Show(content, identifier) which routes via Identifier and avoids needing a Window reference."],"exampleFix":"// before\nApplication.Current.MainWindow.ShowDialog(content); // MainWindow may be null at startup\n\n// after\nvar window = Window.GetWindow(this);\nif (window is not null)\n    await window.ShowDialog(content);\n// or: await DialogHost.Show(content, \"RootDialog\");","handlingStrategy":"validation","validationCode":"if (window is null)\n    throw new ArgumentNullException(nameof(window));\n\nawait window.ShowDialog(content);","typeGuard":"static bool CanShow(Window? window) => window is not null && window.VisualDepthFirstTraversal().OfType<DialogHost>().Any();","tryCatchPattern":null,"preventionTips":["Resolve the Window via Window.GetWindow(this) inside a Loaded handler, not at construction time.","Prefer DialogHost.Show(content, identifier) over Window.ShowDialog to avoid null-Window issues.","Null-check Application.Current.MainWindow before using it."],"tags":["wpf","materialdesigninxaml","argument-null","window","dialog"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}