{"record":{"id":"e0430bc1c58115e8","repo":"dotnet/wpf","slug":"sr-collection-nonull-textdecorationcollection","errorCode":null,"errorMessage":"SR.Collection_NoNull","messagePattern":"SR\\.Collection_NoNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs","lineNumber":119,"sourceCode":"\n        /// <summary>\n        ///     Returns the index of \"value\" in the list\n        /// </summary>\n        public int IndexOf(TextDecoration value)\n        {\n            ReadPreamble();\n\n            return _collection.IndexOf(value);\n        }\n\n        /// <summary>\n        ///     Inserts \"value\" into the list at the specified position\n        /// </summary>\n        public void Insert(int index, TextDecoration value)\n        {\n            if (value == null)\n            {\n                throw new System.ArgumentException(SR.Collection_NoNull);\n            }\n\n            WritePreamble();\n\n            OnFreezablePropertyChanged(/* oldValue = */ null, /* newValue = */ value);\n\n            _collection.Insert(index, value);\n\n\n\n            ++_version;\n            WritePostscript();\n        }\n\n        /// <summary>\n        ///     Removes \"value\" from the list\n        /// </summary>\n        public bool Remove(TextDecoration value)","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs#L101-L137","documentation":"TextDecorationCollection.Insert rejects a null TextDecoration value and throws ArgumentException with SR.Collection_NoNull. WPF freezable collections do not permit null entries because downstream rendering and property-system code assumes every element is a valid TextDecoration. The check runs before WritePreamble so the collection is not mutated.","triggerScenarios":"Calling collection.Insert(index, null) on a TextDecorationCollection, or any API that forwards a null item into Insert (e.g. deserializing a XAML collection with a null entry).","commonSituations":"Building a TextDecorationCollection programmatically from a data source where some rows have no decoration; data binding that yields null items; XAML resource dictionaries that resolve to null.","solutions":["Check the value for null before calling Insert and skip or substitute a default TextDecoration","Use Add/Insert only with non-null instances; construct a new TextDecoration() if you need a placeholder","If the intent is 'no decoration', remove the entry or use TextDecorations prebuilt collections instead of inserting null"],"exampleFix":"// before\ncollection.Insert(0, GetDecoration());\n// after\nvar d = GetDecoration();\nif (d != null) collection.Insert(0, d);","handlingStrategy":"validation","validationCode":"if (index < 0 || index > collection.Count) throw new ArgumentOutOfRangeException(nameof(index));\nif (value == null) throw new ArgumentNullException(nameof(value));","typeGuard":"bool IsValidForInsert(int index, TextDecoration value) => value != null && index >= 0 && index <= collection.Count;","tryCatchPattern":null,"preventionTips":["Null-check items before any add/insert call","Never use null as a placeholder; use new TextDecoration()","Filter nulls out of source data before populating the collection"],"tags":["wpf","argumentexception","null-check","collection"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}