dotnet/wpf · error · ArgumentException

SR.ReferenceIsNull (item.Value)

Error message

SR.ReferenceIsNull (item.Value)

What it means

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.

Solutions

  1. Ensure the object registered for the name is a non-null instance before adding it to the NameScope
  2. Check each KeyValuePair for null Key/Value before passing it to Add
  3. Use Add(string key, object value) only after the scoped element has been constructed

Example fix

// before
nameScope.Add(new KeyValuePair<string, object>("Button1", null));
// after
object target = FindElement("Button1");
if (target != null) nameScope.Add(new KeyValuePair<string, object>("Button1", target));
Defensive patterns

Strategy: validation

Validate before calling

if (pair.Key is null || pair.Value is null) throw new ArgumentException(nameof(pair));
nameScope.Add(pair);

Type guard

static bool IsValidEntry(KeyValuePair<string, object> p) => p.Key is not null && p.Value is not null;

Try / catch

try { nameScope.Add(pair); }
catch (ArgumentException ex) { /* ex.ParamName == "item" */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bf6392014692fc10. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs:150

            if (item.Value != this[item.Key])
            {
                return false;
            }

            return Remove(item.Key);
        }

        public void Add(KeyValuePair<string, object> item)
        {
            if (item.Key is null)
            {
                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, "item.Key"), nameof(item));
            }

            if (item.Value is null)
            {
                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, "item.Value"), nameof(item));
            }

            Add(item.Key, item.Value);
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            if (item.Key is null)
            {
                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, "item.Key"), nameof(item));
            }

            return ContainsKey(item.Key);
        }

        public object this[string key]
        {
            get

View on GitHub (pinned to 81131a70a4)