PrismLibrary/Prism · error · ArgumentNullException
ArgumentNullException(nameof(key))
Error message
ArgumentNullException(nameof(key))
What it means
RegionBehaviorCollection.Add throws ArgumentNullException when the key string is null. The key is used to look up behaviors in the internal dictionary, so it must be a non-null identifier (conventionally the behavior's name).
Solutions
- Pass a non-null key, typically nameof(MyRegionBehavior)
- Define the key as a public const string on the behavior class
- Validate the key before calling Add
Example fix
// before region.Behaviors.Add(null, new MyBehavior()); // after region.Behaviors.Add(nameof(MyBehavior), new MyBehavior());
Defensive patterns
Strategy: validation
Validate before calling
if (key != null && behavior != null) region.Behaviors.Add(key, behavior);
Type guard
bool ValidRegistration(string k, IRegionBehavior b) => !string.IsNullOrEmpty(k) && b != null;
Try / catch
try { region.Behaviors.Add(key, b); } catch (ArgumentNullException ex) when (ex.ParamName == "key") { /* fix key */ } Prevention
- Use nameof(BehaviorType) as key
- Define keys as const strings
- Null-check config-derived keys
When it happens
Trigger: Calling region.Behaviors.Add(null, behavior), often when the key comes from a constant, config value, or typeof expression that resolved to null.
Common situations: Copying behavior registration code where the key constant was deleted or renamed; building the key dynamically from settings that are missing.
Related errors
- ArgumentNullException(nameof(regionBehavior))
- Could not add duplicate behavior with same key.
- ArgumentNullException(nameof(behaviorKey))
- ArgumentNullException(nameof(behaviorType))
- Layout 's Children property is not empty. This control is…
AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15).
Data as JSON: /api/errors/eec860a8222d80bd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs:43
/// Gets the <see cref="IRegionBehavior"/> with the specified key.
/// </summary>
/// <value>The RegionBehavior that's registered with the key.</value>
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>View on GitHub (pinned to 358118cd64)