{"record":{"id":"b5d042aec4ccec41","repo":"dotnet/wpf","slug":"sr-referenceisnull-item-key","errorCode":null,"errorMessage":"SR.ReferenceIsNull (item.Key)","messagePattern":"SR\\.ReferenceIsNull \\(item\\.Key\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs","lineNumber":145,"sourceCode":"        {\n            if (!Contains(item))\n            {\n                return false;\n            }\n\n            if (item.Value != this[item.Key])\n            {\n                return false;\n            }\n\n            return Remove(item.Key);\n        }\n\n        public void Add(KeyValuePair<string, object> item)\n        {\n            if (item.Key is null)\n            {\n                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, \"item.Key\"), nameof(item));\n            }\n\n            if (item.Value is null)\n            {\n                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, \"item.Value\"), nameof(item));\n            }\n\n            Add(item.Key, item.Value);\n        }\n\n        public bool Contains(KeyValuePair<string, object> item)\n        {\n            if (item.Key is null)\n            {\n                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, \"item.Key\"), nameof(item));\n            }\n\n            return ContainsKey(item.Key);","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs#L127-L163","documentation":"NameScope.Add(KeyValuePair<string,object>) throws ArgumentException(SR.ReferenceIsNull) when the pair's Key or Value is null. A namescope entry needs both a non-null name and a non-null scoped object.","triggerScenarios":"Adding a KeyValuePair<string,object> whose Key is null (or Value is null) via the ICollection<KVP> Add implementation, e.g. from LINQ or collection-initializer code with null entries.","commonSituations":"Dictionary enumerations that include null keys, data-binding or test code constructing pairs from nullable sources.","solutions":["Ensure item.Key is a non-empty-safe, non-null string before Add (Value must also be non-null).","Filter out null-keyed/null-valued pairs before adding.","Use RegisterName which gives clearer validation messages."],"exampleFix":"// before\nscope.Add(new KeyValuePair<string, object>(name, element)); // name may be null\n// after\nif (name != null && element != null) scope.Add(new KeyValuePair<string, object>(name, element));","handlingStrategy":"validation","validationCode":"if (item.Key == null || item.Value == null) throw new ArgumentException(\"item\", \"Key and Value must be non-null\");","typeGuard":"bool IsValidPair(KeyValuePair<string, object> p) => p.Key != null && p.Value != null;","tryCatchPattern":"try { nameScope.Add(item); } catch (ArgumentException ex) when (ex.ParamName == \"item\") { /* skip or fix the null-keyed pair */ }","preventionTips":["Filter null entries before adding pairs to a NameScope","Prefer RegisterName over the ICollection Add for clearer errors","Validate dictionary sources that may contain null keys"],"tags":["xaml","namescope","null-argument"],"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"}