dotnet/wpf · error · ArgumentNullException

The translator argument cannot be null.

Error message

The translator argument cannot be null.

What it means

PropertyMap's translator setter validates the value: if translator is null it throws ArgumentNullException with SR.WFI_NullArgument ('The translator argument cannot be null.'). To remove a mapping use Remove or set via the underlying dictionary API semantics, not a null delegate — the setter only accepts a valid PropertyTranslator.

Solutions

  1. Call PropertyMap.Remove(propertyName) (or Reset) to clear a mapping instead of assigning null.
  2. Assign a no-op translator: (control, value) => { } if you need a mapping present but inert.
  3. Guard the assignment: if (translator != null) map[name] = translator;
  4. Check why the delegate is null — verify factory/initialization code paths return a real PropertyTranslator.

Example fix

// before
host.PropertyMap["Background"] = null; // throws
// after
host.PropertyMap.Remove("Background");
Defensive patterns

Strategy: type-guard

Validate before calling

if (translator != null)
    host.PropertyMap[propertyName] = translator;
else
    host.PropertyMap.Remove(propertyName); // null means 'remove', not 'assign null'

Type guard

static bool CanAssignTranslator(PropertyTranslator translator) => translator is not null;

Try / catch

try
{
    host.PropertyMap[propertyName] = translator;
}
catch (ArgumentNullException ex) when (ex.ParamName == "translator")
{
    logger.LogError(ex, "Translator for '{Property}' was null; use Remove to clear", propertyName);
}

Prevention

When it happens

Trigger: Assigning elementHost.PropertyMap["Background"] = null intending to 'clear' a translation; a delegate variable that failed to initialize; a factory method returning null that the caller assigns blindly.

Common situations: Developers assuming indexer assignment with null removes the mapping (it throws instead); conditional delegate creation where all branches returned null; deserialized translator delegates that were null.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMap.cs:86

                ThrowIfPropertyDoesntExistOnSource(propertyName);
                PropertyTranslator value;
                if (_wrappedDictionary.TryGetValue(propertyName, out value))
                {
                    return value;
                }
                return null;
            }
            set
            {
                //Run through our regular invalid property checks, then
                //apply the translator
                if (string.IsNullOrEmpty(propertyName))
                {
                    throw new ArgumentNullException(string.Format(CultureInfo.CurrentCulture, SR.WFI_NullArgument, "propertyName"));
                }
                if (value == null)
                {
                    throw new ArgumentNullException(string.Format(CultureInfo.CurrentCulture, SR.WFI_NullArgument, "translator"));
                }
                ThrowIfPropertyDoesntExistOnSource(propertyName);
                _wrappedDictionary[propertyName] = value;    //This will replace an existing mapping, unlike Add.
                Apply(propertyName);
            }
        }

        /// <summary>
        ///     Returns an ICollection of strings identifying all of the host control 
        ///     properties that have been mapped.  
        /// </summary>
        public ICollection Keys
        {
            get
            {
                return _wrappedDictionary.Keys;
            }
        }

View on GitHub (pinned to 81131a70a4)