{"record":{"id":"a1ffdc89b111b1b2","repo":"dotnet/wpf","slug":"xmlreader-is-null","errorCode":null,"errorMessage":"XmlReader is null","messagePattern":"XmlReader is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlTextReader.cs","lineNumber":70,"sourceCode":"            ArgumentNullException.ThrowIfNull(schemaContext);\n\n            Initialize(xmlReader, schemaContext, null);\n        }\n\n        public XamlTextReader(XmlReader xmlReader, XamlSchemaContext schemaContext, XamlTextReaderSettings settings)\n        {\n            ArgumentNullException.ThrowIfNull(schemaContext);\n\n            Initialize(xmlReader, schemaContext, settings);\n        }\n\n        private void Initialize(XmlReader givenXmlReader, XamlSchemaContext schemaContext, XamlTextReaderSettings settings)\n        {\n            XmlReader myXmlReader;\n\n            if (givenXmlReader == null)\n            {\n                throw new ArgumentNullException(\"XmlReader is null\");\n            }\n\n            _mergedSettings = (settings == null) ? new XamlTextReaderSettings() : new XamlTextReaderSettings(settings);\n\n            //Wrap the xmlreader with a XmlCompatReader instance to apply MarkupCompat rules.\n            if (!_mergedSettings.SkipXmlCompatibilityProcessing)\n            {\n                XmlCompatibilityReader mcReader =\n                        new XmlCompatibilityReader(givenXmlReader,\n                                new IsXmlNamespaceSupportedCallback(IsXmlNamespaceSupported)\n                        );\n                myXmlReader = mcReader;\n            }\n            else\n            {   // Don't wrap the xmlreader with XmlCompatReader.\n                // Useful for uses where users want to keep mc: content in the XamlNode stream.\n                // Or have already processed the markup compat and want that extra perf.\n                // We need to go make sure the parser thinks it knows mc: uri,","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlTextReader.cs#L52-L88","documentation":"ArgumentNullException thrown by XamlTextReader.Initialize (invoked from the XamlTextReader constructors, e.g. the Clone and reader-creation paths used by Stroke-like consumers) when the given XmlReader is null. The XAML text reader requires a real XmlReader to wrap (optionally with markup-compatibility processing), so null input is rejected immediately with the literal paramName \"XmlReader is null\".","triggerScenarios":"Calling a XamlTextReader constructor (or Clone) with a null XmlReader argument — e.g. XmlReader.Create failed silently upstream, a factory method returned null, or a caller passed a disposed/uninitialized reader variable.","commonSituations":"XmlReader.Create over a missing stream/file path configuration; pipeline stages that pass through a nullable reader without checking; cloning a reader whose underlying XmlReader was already released.","solutions":["Ensure the XmlReader is created successfully (XmlReader.Create over a valid stream/URI) before passing it to XamlTextReader.","Null-check the reader at the call site and fail with a clear message identifying the missing source file/stream.","If the reader may legitimately be absent, skip constructing XamlTextReader rather than passing null.","Check that any upstream factory (e.g. stream open, file load) returned non-null before chaining into the reader."],"exampleFix":"// before\nXmlReader xml = TryOpen(path); // may return null\nvar reader = new XamlTextReader(xml, schemaContext, settings); // ArgumentNullException\n// after\nXmlReader xml = TryOpen(path) ?? throw new FileNotFoundException(\"XAML source not found\", path);\nvar reader = new XamlTextReader(xml, schemaContext, settings);","handlingStrategy":"validation","validationCode":"if (xmlReader == null)\n    throw new ArgumentException(\"XmlReader must be created before constructing XamlTextReader.\");","typeGuard":"static bool IsValidReader(XmlReader r) => r is XmlReader { IsDefault: _ } && r is not null; // simplest: r != null","tryCatchPattern":"try { var reader = new XamlTextReader(xmlReader, sc, settings); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"XmlReader is null\") { FailWithSourceDiagnostic(); }","preventionTips":["Null-check the result of XmlReader.Create before chaining into XamlTextReader.","Ensure the source stream/URI exists before opening the XmlReader.","Do not pass disposed or factory-returned-null readers.","Fail fast with a source-path diagnostic rather than forwarding null."],"tags":["xaml","argumentnullexception","xmlreader","null-check"],"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"}