{"record":{"id":"d9d085c1edec41a7","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-xmlparsercontext","errorCode":null,"errorMessage":"ArgumentNullException(nameof(xmlParserContext))","messagePattern":"ArgumentNullException\\(nameof\\(xmlParserContext\\)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/ParserContext.cs","lineNumber":93,"sourceCode":"            _nameScopeStack    = null;\n#endif\n            _xmlLang        = String.Empty;\n            _xmlSpace       = String.Empty;\n        }\n\n\n#if !PBTCOMPILER\n        /// <summary>\n        ///    Constructor that takes the XmlParserContext.\n        ///    A parserContext object will be built based on this.\n        /// </summary>\n        /// <param name=\"xmlParserContext\">xmlParserContext to use</param>\n\n        public ParserContext(XmlParserContext xmlParserContext)\n        {\n            if (xmlParserContext == null)\n            {\n                throw new ArgumentNullException( nameof(xmlParserContext));\n            }\n\n            _xmlLang     = xmlParserContext.XmlLang;\n\n            TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(XmlSpace));\n            if (typeConverter != null)\n                _xmlSpace = (string) typeConverter.ConvertToString(null, TypeConverterHelper.InvariantEnglishUS, xmlParserContext.XmlSpace);\n            else\n                _xmlSpace = String.Empty;\n\n            _xmlnsDictionary = new XmlnsDictionary() ;\n\n            if (xmlParserContext.BaseURI != null && xmlParserContext.BaseURI.Length > 0)\n            {\n                _baseUri = new Uri(xmlParserContext.BaseURI, UriKind.RelativeOrAbsolute);\n            }\n\n            XmlNamespaceManager xmlnsManager = xmlParserContext.NamespaceManager;","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/ParserContext.cs#L75-L111","documentation":"The ParserContext(XmlParserContext) constructor requires a non-null xmlParserContext; passing null throws ArgumentNullException(nameof(xmlParserContext)). The constructor copies XmlLang, XmlSpace and other settings out of the given XmlParserContext, so it cannot proceed without one.","triggerScenarios":"new ParserContext((XmlParserContext)null) or a variable holding a null XmlParserContext passed to the constructor, typically when a preceding XML load failed and a null context propagated.","commonSituations":"Wrapping an XmlParserContext produced by System.Xml APIs that may return null, refactoring where context creation was skipped on an error path, or calling the constructor before a XAML reader was configured.","solutions":["Ensure the XmlParserContext is created (new XmlParserContext(...)) before wrapping it in ParserContext","Add a null check or ArgumentNullException.ThrowIfNull on the value before calling the constructor","Use the parameterless ParserContext() constructor if no XML context is available","Fix upstream code that returns null XmlParserContext on failure"],"exampleFix":"// before\nParserContext pc = new ParserContext(GetXmlParserContext()); // may return null\n// after\nXmlParserContext xmlCtx = GetXmlParserContext() ?? new XmlParserContext(null, null, null, XmlSpace.None);\nParserContext pc = new ParserContext(xmlCtx);","handlingStrategy":"validation","validationCode":"ArgumentNullException.ThrowIfNull(xmlParserContext);\nvar pc = new ParserContext(xmlParserContext);","typeGuard":"ParserContext? TryCreateParserContext(XmlParserContext? ctx) => ctx is null ? null : new ParserContext(ctx);","tryCatchPattern":"try\n{\n    var pc = new ParserContext(xmlParserContext);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"xmlParserContext\")\n{\n    // construct a default ParserContext or abort with a clearer error\n}","preventionTips":["Never pass nullable XmlParserContext values straight into the constructor","Check upstream XML operations for null-returning failure paths first","Use the parameterless ParserContext() when no XML context exists","Enable nullable reference types to catch this at compile time"],"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"}