{"record":{"id":"03f16bd334525b96","repo":"PrismLibrary/Prism","slug":"argumentnullexception-nameof-behaviortype","errorCode":null,"errorMessage":"ArgumentNullException(nameof(behaviorType))","messagePattern":"ArgumentNullException\\(nameof\\(behaviorType\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs","lineNumber":42,"sourceCode":"        {\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;\n            }\n\n            _registeredBehaviors.Add(behaviorKey, behaviorType);\n        }\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs#L24-L60","documentation":"Validation guard in RegionBehaviorFactory.AddIfMissing: the factory resolves the behavior instance from behaviorType via the container; a null Type cannot be instantiated or registered, so ArgumentNullException naming 'behaviorType' is thrown after the null-key check.","triggerScenarios":"Calling AddIfMissing(key, null) — e.g. typeof resolution from a string via Type.GetType that returned null because the assembly-qualified name was wrong.","commonSituations":"Type.GetType with a wrong or incomplete assembly-qualified name (returns null silently); loading behavior types from config; assembly not referenced/loaded in a plugin scenario.","solutions":["Pass typeof(MyBehavior) directly instead of Type.GetType on a string","If using Type.GetType, check for null and include the assembly-qualified name","Ensure the assembly containing the behavior is referenced and loaded"],"exampleFix":"// before\nvar t = Type.GetType(\"MyBehavior\"); // null\nfactory.AddIfMissing(key, t);\n// after\nfactory.AddIfMissing(key, typeof(MyBehavior));","handlingStrategy":"validation","validationCode":"var t = Type.GetType(name);\nif (t == null) throw new InvalidOperationException($\"Type {name} not found\");\nfactory.AddIfMissing(key, t);","typeGuard":"bool ResolvableType(string n) => Type.GetType(n, throwOnError: false) != null;","tryCatchPattern":"try { factory.AddIfMissing(key, t); } catch (ArgumentNullException ex) when (ex.ParamName == \"behaviorType\") { /* resolve type */ }","preventionTips":["Prefer typeof(T) over Type.GetType strings","Use assembly-qualified names when reflecting","Fail fast on null Type resolution"],"tags":["prism","regions","null-check","reflection"],"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"}