PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException(nameof(regionBehavior))

Error message

ArgumentNullException(nameof(regionBehavior))

What it means

Validation guard in RegionBehaviorCollection.Add: the collection indexes behaviors by key, so a null IRegionBehavior instance makes the entry meaningless; ArgumentNullException naming 'regionBehavior' is thrown when the behavior itself is null (null keys are checked separately).

Solutions

  1. Ensure the behavior instance is constructed before Add
  2. Guard: if (behavior != null) region.Behaviors.Add(key, behavior);
  3. Fix DI registration for the IRegionBehavior type

Example fix

// before
region.Behaviors.Add(key, ResolveBehavior()); // may be null
// after
var b = ResolveBehavior();
if (b != null) region.Behaviors.Add(key, b);
Defensive patterns

Strategy: validation

Validate before calling

if (behavior != null) region.Behaviors.Add(key, behavior);

Type guard

bool IsBehavior(object o) => o is IRegionBehavior;

Try / catch

try { region.Behaviors.Add(key, b); } catch (ArgumentNullException ex) when (ex.ParamName == "regionBehavior") { /* resolve behavior */ }

Prevention

When it happens

Trigger: Calling region.Behaviors.Add(key, null), e.g. a factory/DI resolution that returned null or a conditional expression that produced null.

Common situations: DI container not registered for the behavior type; conditional behavior creation returning null when a feature flag is off.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/c70787d117c25fba. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs:46

        public IRegionBehavior this[string key] => behaviors[key];

        /// <summary>
        /// Adds a <see cref="IRegionBehavior"/> to the collection, using the specified key as an indexer.
        /// </summary>
        /// <param name="key">The key that specifies the type of <see cref="IRegionBehavior"/> that's added.</param>
        /// <param name="regionBehavior">The <see cref="IRegionBehavior"/> to add.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown is the <paramref name="key"/> parameter is Null,
        /// or if the <paramref name="regionBehavior"/> parameter is Null.
        /// </exception>
        /// <exception cref="ArgumentException">Thrown if a behavior with the specified Key parameter already exists.</exception>
        public void Add(string key, IRegionBehavior regionBehavior)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            if (regionBehavior == null)
                throw new ArgumentNullException(nameof(regionBehavior));

            if (behaviors.ContainsKey(key))
                throw new ArgumentException("Could not add duplicate behavior with same key.", nameof(key));

            behaviors.Add(key, regionBehavior);
            regionBehavior.Region = region;

            regionBehavior.Attach();
        }

        /// <summary>
        /// Checks if a <see cref="IRegionBehavior"/> with the specified key is already present.
        /// </summary>
        /// <param name="key">The key to use to find a particular <see cref="IRegionBehavior"/>.</param>
        /// <returns></returns>
        public bool ContainsKey(string key) => behaviors.ContainsKey(key);

        /// <summary>

View on GitHub (pinned to 358118cd64)