{"record":{"id":"bf6392014692fc10","repo":"dotnet/wpf","slug":"sr-referenceisnull-item-value","errorCode":null,"errorMessage":"SR.ReferenceIsNull (item.Value)","messagePattern":"SR\\.ReferenceIsNull \\(item\\.Value\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs","lineNumber":150,"sourceCode":"\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);\n        }\n\n        public object this[string key]\n        {\n            get","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs#L132-L168","documentation":"NameScope.Add(KeyValuePair<string,object>) validates each entry of the pair before delegating to Add(string,object). If the value of the KeyValuePair is null, the value would not be a valid registered XAML name scope element, so an ArgumentException is thrown. The name scope must map names to non-null object instances.","triggerScenarios":"Calling Add(KeyValuePair<string,object>) (e.g. via ICollection<KVP> or collection initializer) with a pair whose Value is null; also reachable from the item.Key variant at line 146 when Key is null.","commonSituations":"Deserializing or constructing name scope entries from data sources where values may be missing; collection-initializer syntax with a null element value; tests exercising NameScope's ICollection<string,object> contract.","solutions":["Ensure the object registered for the name is a non-null instance before adding it to the NameScope","Check each KeyValuePair for null Key/Value before passing it to Add","Use Add(string key, object value) only after the scoped element has been constructed"],"exampleFix":"// before\nnameScope.Add(new KeyValuePair<string, object>(\"Button1\", null));\n// after\nobject target = FindElement(\"Button1\");\nif (target != null) nameScope.Add(new KeyValuePair<string, object>(\"Button1\", target));","handlingStrategy":"validation","validationCode":"if (pair.Key is null || pair.Value is null) throw new ArgumentException(nameof(pair));\nnameScope.Add(pair);","typeGuard":"static bool IsValidEntry(KeyValuePair<string, object> p) => p.Key is not null && p.Value is not null;","tryCatchPattern":"try { nameScope.Add(pair); }\ncatch (ArgumentException ex) { /* ex.ParamName == \"item\" */ }","preventionTips":["Never register null scoped elements in a NameScope","Validate KeyValuePair entries before adding","Construct elements before naming them"],"tags":["wpf","system-xaml","namescope","argumentexception"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}