{"record":{"id":"098ad040a228fc56","repo":"dotnet/wpf","slug":"sr-schemacontextnotinitialized-xamlobjectreader","errorCode":null,"errorMessage":"SR.SchemaContextNotInitialized","messagePattern":"SR\\.SchemaContextNotInitialized","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":46,"sourceCode":"\n        public XamlObjectReader(object instance)\n            : this(instance, (XamlObjectReaderSettings)null)\n        {\n        }\n\n        public XamlObjectReader(object instance, XamlObjectReaderSettings settings)\n            : this(instance, new XamlSchemaContext(), settings)\n        {\n        }\n\n        public XamlObjectReader(object instance, XamlSchemaContext schemaContext)\n            : this(instance, schemaContext, null)\n        {\n        }\n\n        public XamlObjectReader(object instance, XamlSchemaContext schemaContext, XamlObjectReaderSettings settings)\n        {\n            this.schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));\n            this.settings = settings ?? new XamlObjectReaderSettings();\n            nodes = new Stack<MarkupInfo>();\n            currentXamlNode = new XamlNode(XamlNode.InternalNodeType.StartOfStream);\n\n            var context = new SerializerContext(schemaContext, this.settings) { RootType = instance?.GetType() };\n\n            var rootObject = ObjectMarkupInfo.ForObject(instance, context, null, true);\n\n            // While we still have name scopes to do, do them. We postpone name scopes so that we can appropriately\n            // resolve references from inside a namescope to objects outside of the name scope (which may also occur\n            // later in the XAML).\n            //\n            while (context.PendingNameScopes.Count > 0)\n            {\n                var recordInfo = context.PendingNameScopes.Dequeue();\n                recordInfo.Resume(context);\n            }\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L28-L64","documentation":"XamlObjectReader requires a non-null XamlSchemaContext to resolve types and members while reading the object graph. The constructor guard here is on schemaContext itself; the message is thrown when the reader's schema context is missing or not initialized, since no XAML type mapping can be performed without it.","triggerScenarios":"Passing null as the XamlSchemaContext argument to XamlObjectReader(object, XamlSchemaContext) or XamlObjectReader(object, XamlSchemaContext, XamlObjectReaderSettings) — directly rejected via ArgumentNullException (XamlObjectReader.cs:46 area). Also occurs on a XamlSchemaContext that was never initialized (not built from reference assemblies) before use.","commonSituations":"Constructing XamlObjectReader programmatically and passing null because the context came from an uninitialized/failed factory call; copying a pattern from overloads that derive the context automatically; DI container injecting a null schema context.","solutions":["Pass a valid XamlSchemaContext, e.g. new XamlSchemaContext() or one obtained from XamlReader.SchemaContext of an existing reader.","Add a null check before constructing the reader so the failure surfaces at the call site with a clear message.","Use the overload XamlObjectReader(object) (or with settings) that constructs a default schema context for you when no custom context is needed."],"exampleFix":"// before\nvar reader = new XamlObjectReader(instance, null); // throws\n// after\nvar schemaContext = new XamlSchemaContext();\nvar reader = new XamlObjectReader(instance, schemaContext);","handlingStrategy":"type-guard","validationCode":"if (schemaContext is null) throw new ArgumentNullException(nameof(schemaContext));\nvar reader = new XamlObjectReader(instance, schemaContext);","typeGuard":"bool HasValidSchemaContext(XamlSchemaContext ctx) => ctx is not null && ctx.ReferenceAssemblies is not null;\n","tryCatchPattern":"try { var reader = new XamlObjectReader(instance, ctx); }\ncatch (ArgumentNullException) { ctx = new XamlSchemaContext(); /* retry with default context */ }","preventionTips":["Prefer the XamlObjectReader(object) overload when you have no custom context requirements.","Centralize schema-context creation in one factory that never returns null.","Null-check context-producing calls before passing their result into readers."],"tags":["xaml","null-argument","schema-context"],"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"}