dotnet/wpf · error · ArgumentException
SR.ReferenceIsNull (item.Key)
Error message
SR.ReferenceIsNull (item.Key)
What it means
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.
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.
Example fix
// before scope.Add(new KeyValuePair<string, object>(name, element)); // name may be null // after if (name != null && element != null) scope.Add(new KeyValuePair<string, object>(name, element));
Defensive patterns
Strategy: validation
Validate before calling
if (item.Key == null || item.Value == null) throw new ArgumentException("item", "Key and Value must be non-null"); Type guard
bool IsValidPair(KeyValuePair<string, object> p) => p.Key != null && p.Value != null;
Try / catch
try { nameScope.Add(item); } catch (ArgumentException ex) when (ex.ParamName == "item") { /* skip or fix the null-keyed pair */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: Dictionary enumerations that include null keys, data-binding or test code constructing pairs from nullable sources.
Related errors
- ArgumentNullException(nameof(arrayType))
- ArgumentNullException(nameof(clrNamespace))
- ArgumentNullException(nameof(contentType))
- ArgumentNullException(nameof(loaderType))
- ArgumentNullException(nameof(member))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b5d042aec4ccec41.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs:145
{
if (!Contains(item))
{
return false;
}
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);View on GitHub (pinned to 81131a70a4)