{"record":{"id":"b42a1f637690b8b9","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-parsercontext","errorCode":null,"errorMessage":"ArgumentNullException(nameof(parserContext))","messagePattern":"ArgumentNullException\\(nameof\\(parserContext\\)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/ParserContext.cs","lineNumber":420,"sourceCode":"        /// </summary>\n        /// <param name=\"parserContext\">ParserContext to Convert</param>\n        /// <returns>XmlParserContext</returns>\n        public static implicit operator XmlParserContext(ParserContext parserContext)\n        {\n            return ParserContext.ToXmlParserContext(parserContext);\n        }\n\n\n        /// <summary>\n        ///    Operator for Converting a ParserContext to an XmlParserContext\n        /// </summary>\n        /// <param name=\"parserContext\">ParserContext to Convert</param>\n        /// <returns>XmlParserContext</returns>\n        public static XmlParserContext ToXmlParserContext(ParserContext parserContext)\n        {\n            if (parserContext == null)\n            {\n                throw new ArgumentNullException( nameof(parserContext));\n            }\n\n            XmlNamespaceManager xmlnsMgr = new XmlNamespaceManager(new NameTable());\n            XmlSpace xmlSpace = System.Xml.XmlSpace.None;\n\n            if (parserContext.XmlSpace != null && parserContext.XmlSpace.Length != 0)\n            {\n                TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(System.Xml.XmlSpace));\n                if (null != typeConverter)\n                {\n                    try\n                    {\n                        xmlSpace = (System.Xml.XmlSpace)typeConverter.ConvertFromString(null, TypeConverterHelper.InvariantEnglishUS, parserContext.XmlSpace);\n                    }\n                    catch (System.FormatException)  // If it's not a valid space value, ignore it\n                    {\n                        xmlSpace = System.Xml.XmlSpace.None;\n                    }","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/ParserContext.cs#L402-L438","documentation":"The static ParserContext.ToXmlParserContext(ParserContext) converter requires a non-null parserContext and throws ArgumentNullException(nameof(parserContext)) when given null. It translates a XAML ParserContext into a System.Xml XmlParserContext (namespace manager, XmlLang, XmlSpace), which is impossible without a source context.","triggerScenarios":"Calling ParserContext.ToXmlParserContext(null), or passing a ParserContext variable that was never initialized or was set to null after a failed load, commonly in custom XAML reader/pipeline code.","commonSituations":"Custom XamlReader pipelines converting contexts between System.Xaml and System.Xml, error paths where a ParserContext factory returned null, and unit tests exercising edge cases.","solutions":["Pass a valid ParserContext instance (use new ParserContext() if defaults suffice)","Null-check the context before calling ToXmlParserContext","Fix the producer of the ParserContext so it never returns null","Wrap in ArgumentNullException.ThrowIfNull(parserContext) guard at the call site"],"exampleFix":"// before\nParserContext pc = CreateParserContext(); // may be null\nXmlParserContext xmlCtx = ParserContext.ToXmlParserContext(pc);\n// after\nParserContext pc = CreateParserContext() ?? new ParserContext();\nXmlParserContext xmlCtx = ParserContext.ToXmlParserContext(pc);","handlingStrategy":"validation","validationCode":"ArgumentNullException.ThrowIfNull(parserContext);\nvar xmlCtx = ParserContext.ToXmlParserContext(parserContext);","typeGuard":"XmlParserContext? TryToXmlParserContext(ParserContext? ctx) => ctx is null ? null : ParserContext.ToXmlParserContext(ctx);","tryCatchPattern":"try\n{\n    var xmlCtx = ParserContext.ToXmlParserContext(parserContext);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"parserContext\")\n{\n    xmlCtx = ParserContext.ToXmlParserContext(new ParserContext());\n}","preventionTips":["Ensure ParserContext-producing code never returns null; default to new ParserContext()","Null-check before converting in custom XAML reader pipelines","Keep context construction and conversion close together to avoid null drift","Enable nullable reference types so nullability is enforced statically"],"tags":["wpf","argument-null-exception","parser-context","xaml"],"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"}