{"record":{"id":"c70787d117c25fba","repo":"PrismLibrary/Prism","slug":"argumentnullexception-nameof-regionbehavior","errorCode":null,"errorMessage":"ArgumentNullException(nameof(regionBehavior))","messagePattern":"ArgumentNullException\\(nameof\\(regionBehavior\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs","lineNumber":46,"sourceCode":"        public IRegionBehavior this[string key] => behaviors[key];\n\n        /// <summary>\n        /// Adds a <see cref=\"IRegionBehavior\"/> to the collection, using the specified key as an indexer.\n        /// </summary>\n        /// <param name=\"key\">The key that specifies the type of <see cref=\"IRegionBehavior\"/> that's added.</param>\n        /// <param name=\"regionBehavior\">The <see cref=\"IRegionBehavior\"/> to add.</param>\n        /// <exception cref=\"ArgumentNullException\">\n        /// Thrown is the <paramref name=\"key\"/> parameter is Null,\n        /// or if the <paramref name=\"regionBehavior\"/> parameter is Null.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">Thrown if a behavior with the specified Key parameter already exists.</exception>\n        public void Add(string key, IRegionBehavior regionBehavior)\n        {\n            if (key == null)\n                throw new ArgumentNullException(nameof(key));\n\n            if (regionBehavior == null)\n                throw new ArgumentNullException(nameof(regionBehavior));\n\n            if (behaviors.ContainsKey(key))\n                throw new ArgumentException(\"Could not add duplicate behavior with same key.\", nameof(key));\n\n            behaviors.Add(key, regionBehavior);\n            regionBehavior.Region = region;\n\n            regionBehavior.Attach();\n        }\n\n        /// <summary>\n        /// Checks if a <see cref=\"IRegionBehavior\"/> with the specified key is already present.\n        /// </summary>\n        /// <param name=\"key\">The key to use to find a particular <see cref=\"IRegionBehavior\"/>.</param>\n        /// <returns></returns>\n        public bool ContainsKey(string key) => behaviors.ContainsKey(key);\n\n        /// <summary>","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs#L28-L64","documentation":"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).","triggerScenarios":"Calling region.Behaviors.Add(key, null), e.g. a factory/DI resolution that returned null or a conditional expression that produced null.","commonSituations":"DI container not registered for the behavior type; conditional behavior creation returning null when a feature flag is off.","solutions":["Ensure the behavior instance is constructed before Add","Guard: if (behavior != null) region.Behaviors.Add(key, behavior);","Fix DI registration for the IRegionBehavior type"],"exampleFix":"// before\nregion.Behaviors.Add(key, ResolveBehavior()); // may be null\n// after\nvar b = ResolveBehavior();\nif (b != null) region.Behaviors.Add(key, b);","handlingStrategy":"validation","validationCode":"if (behavior != null) region.Behaviors.Add(key, behavior);","typeGuard":"bool IsBehavior(object o) => o is IRegionBehavior;","tryCatchPattern":"try { region.Behaviors.Add(key, b); } catch (ArgumentNullException ex) when (ex.ParamName == \"regionBehavior\") { /* resolve behavior */ }","preventionTips":["Verify DI registration for behavior types","Null-check factory/resolver results","Fail fast at startup if a required behavior cannot be created"],"tags":["prism","regions","null-check","collection"],"backgroundTag":"null-argument","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}