{"record":{"id":"a27851ccd3d3f15b","repo":"PrismLibrary/Prism","slug":"argumentnullexception-nameof-behaviorkey","errorCode":null,"errorMessage":"ArgumentNullException(nameof(behaviorKey))","messagePattern":"ArgumentNullException\\(nameof\\(behaviorKey\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs","lineNumber":37,"sourceCode":"        /// <summary>\n        /// Initializes a new instance of <see cref=\"RegionBehaviorFactory\"/>.\n        /// </summary>\n        /// <param name=\"container\"><see cref=\"IContainerExtension\"/> used to create the instance of the behavior from its <see cref=\"Type\"/>.</param>\n        public RegionBehaviorFactory(IContainerExtension container)\n        {\n            _container = container;\n        }\n\n        /// <summary>\n        /// Adds a particular type of RegionBehavior if it was not already registered. The <paramref name=\"behaviorKey\"/> string is used to check if the behavior is already present\n        /// </summary>\n        /// <param name=\"behaviorKey\">The behavior key that's used to find if a certain behavior is already added.</param>\n        /// <param name=\"behaviorType\">Type of the behavior to add.</param>\n        public void AddIfMissing(string behaviorKey, Type behaviorType)\n        {\n            if (behaviorKey == null)\n            {\n                throw new ArgumentNullException(nameof(behaviorKey));\n            }\n\n            if (behaviorType == null)\n            {\n                throw new ArgumentNullException(nameof(behaviorType));\n            }\n\n            // Check if the type is a IRegionBehavior\n            if (!typeof(IRegionBehavior).IsAssignableFrom(behaviorType))\n            {\n                throw new ArgumentException(\n                    string.Format(Thread.CurrentThread.CurrentCulture, Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior, behaviorType.Name), nameof(behaviorType));\n            }\n\n            // Only add the behaviorKey if it doesn't already exists.\n            if (_registeredBehaviors.ContainsKey(behaviorKey))\n            {\n                return;","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs#L19-L55","documentation":"Validation guard in RegionBehaviorFactory.AddIfMissing: the behaviorKey string is used both to check whether a behavior type is already registered and as the registration key; a null key cannot identify a behavior, so ArgumentNullException naming 'behaviorKey' is thrown before any registration check.","triggerScenarios":"Calling AddIfMissing(null, typeof(MyBehavior)) in ConfigureDefaultRegionBehaviors, typically from a null constant or nameof() on a missing member.","commonSituations":"Registering custom behaviors at app startup with a key built from configuration; refactoring removed the key constant.","solutions":["Pass a non-null key, e.g. nameof(MyBehavior)","Declare the key as a const string on the behavior class","Check the argument at the call site in ConfigureDefaultRegionBehaviors"],"exampleFix":"// before\nfactory.AddIfMissing(null, typeof(MyBehavior));\n// after\nfactory.AddIfMissing(nameof(MyBehavior), typeof(MyBehavior));","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(key)) throw new ArgumentException(\"key required\"); factory.AddIfMissing(key, behaviorType);","typeGuard":"bool ValidKey(string k) => !string.IsNullOrEmpty(k);","tryCatchPattern":"try { factory.AddIfMissing(key, t); } catch (ArgumentNullException ex) when (ex.ParamName == \"behaviorKey\") { /* supply key */ }","preventionTips":["Use nameof as key","Const string keys on behavior classes","Validate startup registration inputs"],"tags":["prism","regions","null-check","factory"],"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"}