{"record":{"id":"f7133be15446f99f","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-schemacontext","errorCode":null,"errorMessage":"ArgumentNullException(nameof(schemaContext))","messagePattern":"ArgumentNullException\\(nameof\\(schemaContext\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlType.cs","lineNumber":41,"sourceCode":"        private readonly IList<XamlType> _typeArguments;\n\n        // Thread safety: if setting outside ctor, do an interlocked compare against null\n        private TypeReflector _reflector;\n\n        /// <summary>\n        /// Lazy init: NullableReference.IsSet is null when not initialized\n        /// </summary>\n        private NullableReference<Type> _underlyingType;\n\n        // Lazy init: null until initialized\n        // Thread safety: idempotent, assignment races are okay; do not assign incomplete values\n        private ReadOnlyCollection<string> _namespaces;\n        private ThreeValuedBool _isNameValid;\n\n        protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)\n        {\n            _name = typeName ?? throw new ArgumentNullException(nameof(typeName));\n            _schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));\n            _typeArguments = GetTypeArguments(typeArguments);\n        }\n\n        public XamlType(string unknownTypeNamespace, string unknownTypeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)\n        {\n            ArgumentNullException.ThrowIfNull(unknownTypeNamespace);\n\n            _name = unknownTypeName ?? throw new ArgumentNullException(nameof(unknownTypeName));\n            _namespaces = new ReadOnlyCollection<string>(new string[] { unknownTypeNamespace });\n            _schemaContext = schemaContext ?? throw new ArgumentNullException(nameof(schemaContext));\n            _typeArguments = GetTypeArguments(typeArguments);\n            _reflector = TypeReflector.UnknownReflector;\n        }\n\n        public XamlType(Type underlyingType, XamlSchemaContext schemaContext)\n            :this(underlyingType, schemaContext, null)\n        {\n        }","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlType.cs#L23-L59","documentation":"The protected XamlType(string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext) constructor requires a non-null XamlSchemaContext. XamlType relies on the schema context for type lookup, namespace resolution, and settings such as SupportMarkupExtensionsWithDuplicateArity, so null is rejected with ArgumentNullException.","triggerScenarios":"Constructing a derived XamlType and passing null as schemaContext, e.g. because the context is created lazily after the type or a field holding it was never initialized.","commonSituations":"Custom schema/type provider code where the schema context field is uninitialized during early construction; unit tests constructing XamlType subclasses without a context; ordering issues in static initialization.","solutions":["Create the XamlSchemaContext (e.g. new XamlSchemaContext() or an XamlXmlReaderSettings-derived context) before constructing the XamlType","Pass an existing context instance from the reader/writer or schema provider","Add constructor ordering so the context is initialized first"],"exampleFix":"// before\nvar myType = new MyXamlType(\"MyType\", null, null); // schemaContext null\n// after\nvar schemaContext = new XamlSchemaContext();\nvar myType = new MyXamlType(\"MyType\", null, schemaContext);","handlingStrategy":"type-guard","validationCode":"if (schemaContext is null) throw new InvalidOperationException(\"XamlSchemaContext required\");","typeGuard":"bool ok = schemaContext is not null;","tryCatchPattern":"try { var t = new MyXamlType(name, args, ctx); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"schemaContext\") { /* initialize context */ }","preventionTips":["Create the XamlSchemaContext before any XamlType construction","Reuse one context per reader/writer session","Initialize context fields before base() calls where possible"],"tags":["argumentnullexception","constructor","schemacontext","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"}