MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentNullException
theme
Error message
theme
What it means
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.
Source
Thrown at src/MaterialDesignThemes.Wpf/PaletteHelper.cs:14
namespace MaterialDesignThemes.Wpf;
public class PaletteHelper
{
public virtual Theme GetTheme()
{
if (Application.Current is null)
throw new InvalidOperationException($"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.");
return GetResourceDictionary().GetTheme();
}
public virtual void SetTheme(Theme theme)
{
if (theme is null) throw new ArgumentNullException(nameof(theme));
if (Application.Current is null)
throw new InvalidOperationException($"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.");
GetResourceDictionary().SetTheme(theme);
RecreateThemeDictionaries();
}
public virtual IThemeManager? GetThemeManager()
{
if (Application.Current is null)
throw new InvalidOperationException($"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.");
return GetResourceDictionary().GetThemeManager();
}
private static ResourceDictionary GetResourceDictionary()
=> Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ??
Application.Current.Resources;
View on GitHub (pinned to 98edec3a0b)
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.
Example fix
// before helper.SetTheme(loadedTheme /* may be null */); // after helper.SetTheme(loadedTheme ?? Theme.Create(PrimaryColor.Blue, SecondaryColor.Amber));
Defensive patterns
Strategy: validation
Validate before calling
if (theme is null) throw new ArgumentNullException(nameof(theme)); helper.SetTheme(theme);
Type guard
theme is not null
Prevention
- Build themes via Theme.Create and validate before passing.
- When loading themes from settings, supply a default rather than propagating null.
When it happens
Trigger: Calling PaletteHelper.SetTheme(null), or passing a theme that a builder/factory returned null for.
Common situations: A Theme constructed from user input that failed to build; a refactor that dropped the Theme argument; a deserialization path that yielded null.
Related errors
- Cannot get theme outside of a WPF application. Use {nameof(R
- Cannot set theme outside of a WPF application. Use {nameof(R
- Cannot get ThemeManager outside of a WPF application. Use {n
- Non primary hues provided.
- Entry {entry.Key} was not of type Color
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/a6e334ad5cb0812f.
Report an issue: GitHub.