{"record":{"id":"d93640827187281d","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-clrnamespace","errorCode":null,"errorMessage":"ArgumentNullException(nameof(clrNamespace))","messagePattern":"ArgumentNullException\\(nameof\\(clrNamespace\\)\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs","lineNumber":143,"sourceCode":"        /// </param>\n        /// <param name=\"clrNamespace\">\n        /// The \"System.Windows.ComponentModel\" argument in the mapping PI example.\n        /// </param>\n        /// <param name=\"assemblyName\">\n        /// The \"PresentationFramework\" argument in the mapping PI example.\n        /// </param>\n        public void AddMappingProcessingInstruction(\n            string  xmlNamespace,\n            string  clrNamespace,\n            string  assemblyName )\n        {\n            if( null == xmlNamespace )\n            {\n                throw new ArgumentNullException(nameof(xmlNamespace));\n            }\n            if( null == clrNamespace )\n            {\n                throw new ArgumentNullException(nameof(clrNamespace));\n            }\n            if( null == assemblyName )\n            {\n                throw new ArgumentNullException(nameof(assemblyName));\n            }\n\n            // Parameter validation : Check for String.Empty as well?\n\n            // Add mapping to the table keyed by xmlNamespace\n            ClrNamespaceAssemblyPair pair = new ClrNamespaceAssemblyPair(clrNamespace, assemblyName);\n            PITable[xmlNamespace] = pair;\n\n            // Add mapping to the table keyed by assembly and clrnamespace\n            string upperAssemblyName = assemblyName.ToUpper(\n                                              TypeConverterHelper.InvariantEnglishUS);\n            String fullName = $\"{clrNamespace}#{upperAssemblyName}\";\n\n            _piReverseTable[fullName] = xmlNamespace;","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs#L125-L161","documentation":"XamlTypeMapper.AddMappingProcessingInstruction requires a non-null clrNamespace: the CLR namespace portion of the mapping (e.g. \"System.Windows.Controls\"). It throws ArgumentNullException when clrNamespace is null, because a mapping with no target CLR namespace is meaningless for type resolution.","triggerScenarios":"Calling mapper.AddMappingProcessingInstruction(xmlNamespace, null, assemblyName) — the clr-namespace portion is absent, e.g. parsing \"clr-namespace:;assembly=X\" or a mapping record built from a PI missing its clr-namespace part.","commonSituations":"Hand-edited XAML mapping processing instructions with empty clr-namespace, config-driven mapping tables missing the namespace column, or code generators emitting mappings with uninitialized namespace strings.","solutions":["Supply the CLR namespace string, e.g. mapper.AddMappingProcessingInstruction(ns, \"MyCompany.MyApp.Controls\", \"MyAssembly\").","Parse the clr-namespace from the mapping URI and reject/handle mappings where it is missing before calling.","Catch ArgumentNullException at the mapping-registration boundary and log which mapping entry was malformed."],"exampleFix":"// before\nmapper.AddMappingProcessingInstruction(xmlns, clrNsFromUri, assembly); // clrNsFromUri == null\n// after\nif (string.IsNullOrEmpty(clrNsFromUri))\n    throw new XamlParseException($\"Mapping '{xmlns}' has no clr-namespace\");\nmapper.AddMappingProcessingInstruction(xmlns, clrNsFromUri, assembly);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(clrNamespace))\n    throw new ConfigurationErrorsException($\"Mapping '{xmlNamespace}' is missing its clr-namespace\");","typeGuard":"static bool TryGetClrNamespace(string mappingUri, out string clrNamespace)\n{\n    clrNamespace = null;\n    const string prefix = \"clr-namespace:\";\n    if (mappingUri == null || !mappingUri.StartsWith(prefix)) return false;\n    int end = mappingUri.IndexOf(';');\n    clrNamespace = end < 0 ? mappingUri.Substring(prefix.Length) : mappingUri.Substring(prefix.Length, end - prefix.Length);\n    return clrNamespace.Length > 0;\n}","tryCatchPattern":"try\n{\n    mapper.AddMappingProcessingInstruction(xmlNamespace, clrNamespace, assemblyName);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"clrNamespace\")\n{\n    throw new ConfigurationErrorsException($\"Mapping '{xmlNamespace}' has no clr-namespace\", ex);\n}","preventionTips":["Parse 'clr-namespace:' URIs strictly and reject empty namespace segments","Keep mapping metadata in a schema-validated config so clr-namespace is required","Round-trip test: after AddMappingProcessingInstruction, GetType should resolve a known type"],"tags":["wpf","xaml","argument-null","namespace-mapping"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}