{"record":{"id":"e7ec0d0980649556","repo":"AvaloniaUI/Avalonia","slug":"resources","errorCode":null,"errorMessage":"Resources","messagePattern":"Resources","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Styles.cs","lineNumber":63,"sourceCode":"            private set\n            {\n                if (_owner != value)\n                {\n                    _owner = value;\n                    OwnerChanged?.Invoke(this, EventArgs.Empty);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets or sets a dictionary of style resources.\n        /// </summary>\n        public IResourceDictionary Resources\n        {\n            get => _resources ?? (Resources = new ResourceDictionary());\n            set\n            {\n                value = value ?? throw new ArgumentNullException(nameof(Resources));\n\n                var currentOwner = Owner;\n\n                if (currentOwner is not null)\n                {\n                    _resources?.RemoveOwner(currentOwner);\n                }\n\n                _resources = value;\n\n                if (currentOwner is not null)\n                {\n                    _resources.AddOwner(currentOwner);\n                }\n            }\n        }\n\n        bool ICollection<IStyle>.IsReadOnly => false;","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Styles.cs#L45-L81","documentation":"The Styles.Resources property assigns the IResourceDictionary backing this Styles collection. The getter lazily initializes a ResourceDictionary (get => _resources ?? (Resources = new ResourceDictionary())), so a null dictionary is never a valid state. The setter throws ArgumentNullException because a null value would break lazy initialization and leave resource lookups with no dictionary to query.","triggerScenarios":"Assigning styles.Resources = null directly in C#, or a binding/XAML expression that resolves Resources to null (e.g. Resources=\"{x:Null}\"). Also reachable via code paths that intend to 'clear' resources by nulling them.","commonSituations":"Attempted resource clearing via null assignment instead of clearing the dictionary contents; data-bound resource dictionaries whose source becomes null; migration of older code that assumed nullable Resources; XAML authored with explicit {x:Null}.","solutions":["To clear resources, assign an empty dictionary: styles.Resources = new ResourceDictionary();","If a dictionary is already assigned, call styles.Resources.Clear() to remove entries without replacing the dictionary.","Remove any Resources=\"{x:Null}\" from XAML and ensure bindings that feed Resources never yield null (use a fallback value of an empty ResourceDictionary)."],"exampleFix":"// before\nstyles.Resources = null;\n\n// after\nstyles.Resources = new ResourceDictionary();","handlingStrategy":"validation","validationCode":"// Never assign null; assign an empty dictionary to clear.\nstyles.Resources = newResources ?? new ResourceDictionary();","typeGuard":"static bool IsValidResourceDictionary(IResourceDictionary? dict) => dict is not null;","tryCatchPattern":null,"preventionTips":["To clear resources, call styles.Resources.Clear() or assign new ResourceDictionary().","Ensure any binding feeding Resources has FallbackValue set to an empty ResourceDictionary.","Remove Resources=\"{x:Null}\" from XAML.","Treat Resources as non-nullable in code annotations ([NotNull]) to surface accidental nulls at analysis time."],"tags":["avalonia","styling","resources","null-argument","resource-dictionary"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}