{"record":{"id":"a6e334ad5cb0812f","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"theme","errorCode":null,"errorMessage":"theme","messagePattern":"theme","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/PaletteHelper.cs","lineNumber":14,"sourceCode":"﻿namespace MaterialDesignThemes.Wpf;\n\npublic class PaletteHelper\n{\n    public virtual Theme GetTheme()\n    {\n        if (Application.Current is null)\n            throw new InvalidOperationException($\"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.\");\n        return GetResourceDictionary().GetTheme();\n    }\n\n    public virtual void SetTheme(Theme theme)\n    {\n        if (theme is null) throw new ArgumentNullException(nameof(theme));\n        if (Application.Current is null)\n            throw new InvalidOperationException($\"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.\");\n\n        GetResourceDictionary().SetTheme(theme);\n        RecreateThemeDictionaries();\n    }\n\n    public virtual IThemeManager? GetThemeManager()\n    {\n        if (Application.Current is null)\n            throw new InvalidOperationException($\"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.\");\n        return GetResourceDictionary().GetThemeManager();\n    }\n\n    private static ResourceDictionary GetResourceDictionary()\n        => Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ??\n            Application.Current.Resources;\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/PaletteHelper.cs#L1-L32","documentation":"SetTheme writes a Theme into the application resources but first validates that the Theme argument is non-null, throwing ArgumentNullException(nameof(theme)). This guard fires before the Application.Current check, so it triggers regardless of whether a WPF application is running.","triggerScenarios":"Calling PaletteHelper.SetTheme(null), or passing a theme that a builder/factory returned null for.","commonSituations":"A Theme constructed from user input that failed to build; a refactor that dropped the Theme argument; a deserialization path that yielded null.","solutions":["Build the Theme with Theme.Create(...) or modify an existing GetTheme() result before passing it.","Null-check the theme at the call site before SetTheme.","If loading from settings, validate the deserialized theme and fall back to a default Theme rather than null."],"exampleFix":"// before\nhelper.SetTheme(loadedTheme /* may be null */);\n// after\nhelper.SetTheme(loadedTheme ?? Theme.Create(PrimaryColor.Blue, SecondaryColor.Amber));","handlingStrategy":"validation","validationCode":"if (theme is null) throw new ArgumentNullException(nameof(theme));\nhelper.SetTheme(theme);","typeGuard":"theme is not null","tryCatchPattern":null,"preventionTips":["Build themes via Theme.Create and validate before passing.","When loading themes from settings, supply a default rather than propagating null."],"tags":["wpf","theming","argument-null","palette-helper"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}