{"record":{"id":"b2477ca3212e8d5a","repo":"dotnet/wpf","slug":"sr-collectioncannotcontainnulls-xamlschemacontext","errorCode":null,"errorMessage":"SR.CollectionCannotContainNulls","messagePattern":"SR\\.CollectionCannotContainNulls","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs","lineNumber":295,"sourceCode":"            if (xamlTypeName.Name is null)\n            {\n                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, \"xamlTypeName.Name\"), nameof(xamlTypeName));\n            }\n\n            if (xamlTypeName.Namespace is null)\n            {\n                throw new ArgumentException(SR.Format(SR.ReferenceIsNull, \"xamlTypeName.Namespace\"), nameof(xamlTypeName));\n            }\n\n            XamlType[] typeArgs = null;\n            if (xamlTypeName.HasTypeArgs)\n            {\n                typeArgs = new XamlType[xamlTypeName.TypeArguments.Count];\n                for (int i = 0; i < xamlTypeName.TypeArguments.Count; i++)\n                {\n                    if (xamlTypeName.TypeArguments[i] is null)\n                    {\n                        throw new ArgumentException(SR.Format(SR.CollectionCannotContainNulls, \"xamlTypeName.TypeArguments\"));\n                    }\n\n                    typeArgs[i] = GetXamlType(xamlTypeName.TypeArguments[i]);\n                    if (typeArgs[i] is null)\n                    {\n                        return null;\n                    }\n                }\n            }\n\n            return GetXamlType(xamlTypeName.Namespace, xamlTypeName.Name, typeArgs);\n        }\n\n        protected internal virtual XamlType GetXamlType(string xamlNamespace, string name, params XamlType[] typeArguments)\n        {\n            ArgumentNullException.ThrowIfNull(xamlNamespace);\n            ArgumentNullException.ThrowIfNull(name);\n            if (typeArguments is not null)","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs#L277-L313","documentation":"When resolving a generic XamlTypeName, each entry of xamlTypeName.TypeArguments must be non-null; a null type argument makes the generic type unresolvable, so ArgumentException with CollectionCannotContainNulls is thrown.","triggerScenarios":"Calling GetXamlType on a XamlTypeName with TypeArguments populated including a null element (e.g. building the list by index and leaving a slot null).","commonSituations":"Programmatic construction of generic type names (List<T>, Dictionary<K,V> in XAML) where one argument failed to resolve to a XamlType beforehand.","solutions":["Filter or assert all TypeArguments are non-null before calling GetXamlType","Resolve each type argument through GetXamlType first and handle null returns","Log which index was null to find the producer of the bad argument"],"exampleFix":"// before\ntn.TypeArguments.Add(null);\n// after\nforeach (var arg in args) if (arg != null) tn.TypeArguments.Add(arg);","handlingStrategy":"validation","validationCode":"if (xamlTypeName.TypeArguments?.Contains(null) == true) throw new ArgumentException(\"TypeArguments must not contain null entries\");","typeGuard":"bool HasValidTypeArgs(XamlTypeName t) => t?.TypeArguments is null || t.TypeArguments.All(a => a is not null);","tryCatchPattern":"try { var xt = ctx.GetXamlType(xtn); } catch (ArgumentException ex) when (ex.Message.Contains(\"TypeArguments\")) { /* handle null generic argument */ }","preventionTips":["Resolve each generic argument to a XamlType before composing","Assert generic arity matches supplied arguments"],"tags":["xaml","generics","argument-validation","wpf"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}