{"record":{"id":"e5463a467f4df1a8","repo":"dotnet/wpf","slug":"sr-collectioncannotcontainnulls","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/Schema/XamlDirective.cs","lineNumber":44,"sourceCode":"            }\n#endif\n            _xamlNamespaces = immutableXamlNamespaces;\n            _allowedLocation = allowedLocation;\n        }\n\n        public XamlDirective(IEnumerable<string> xamlNamespaces, string name, XamlType xamlType,\n            XamlValueConverter<TypeConverter> typeConverter, AllowedMemberLocations allowedLocation)\n            : base(name, new MemberReflector(xamlType, typeConverter))\n        {\n            ArgumentNullException.ThrowIfNull(xamlType);\n            ArgumentNullException.ThrowIfNull(xamlNamespaces);\n\n            List<string> nsList = new List<string>(xamlNamespaces);\n            foreach (string ns in nsList)\n            {\n                if (ns is null)\n                {\n                    throw new ArgumentException(SR.CollectionCannotContainNulls, nameof(xamlNamespaces));\n                }\n            }\n\n            _xamlNamespaces = nsList.AsReadOnly();\n            _allowedLocation = allowedLocation;\n        }\n\n        public XamlDirective(string xamlNamespace, string name)\n            :base(name, null)\n        {\n            ArgumentNullException.ThrowIfNull(xamlNamespace);\n\n            _xamlNamespaces = new ReadOnlyCollection<string>(new string[] { xamlNamespace });\n            _allowedLocation = AllowedMemberLocations.Any;\n        }\n\n        public AllowedMemberLocations AllowedLocation { get { return _allowedLocation; } }\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlDirective.cs#L26-L62","documentation":"The XamlDirective constructor validates that the xamlNamespaces collection contains no null entries before storing it as a read-only list. A null namespace string would make directive resolution ambiguous, so ArgumentException(SR.CollectionCannotContainNulls, nameof(xamlNamespaces)) is thrown eagerly.","triggerScenarios":"Calling `new XamlDirective(xamlNamespaces, name, ...)` where the IEnumerable<string> passed as xamlNamespaces contains a null element, e.g. `new XamlDirective(new[] { null }, \"MyDirective\", ...)` or a list built with `list.Add(null)`.","commonSituations":"Programmatically building a namespace list where entries come from parsing or configuration and a missing namespace string yields null instead of empty; interop code converting non-.NET strings to XAML namespace strings.","solutions":["Sanitize the collection before constructing: remove or replace null entries with the correct namespace string.","Use string.Empty for an empty namespace if that was the intent (e.g. CLR default namespace).","Add a guard that throws with a clear message identifying which entry is null so callers can fix the source data."],"exampleFix":"// before\nvar directive = new XamlDirective(namespacesFromConfig, \"Key\", XamlLanguage.String, null, AllowedMemberLocations.Any);\n// namespacesFromConfig contains null\n// after\nvar cleanNamespaces = namespacesFromConfig.Where(ns => ns is not null).ToList();\nvar directive = new XamlDirective(cleanNamespaces, \"Key\", XamlLanguage.String, null, AllowedMemberLocations.Any);","handlingStrategy":"validation","validationCode":"if (xamlNamespaces is not null && xamlNamespaces.Any(ns => ns is null)) throw new ArgumentException(\"xamlNamespaces contains null entries\");","typeGuard":"static bool HasNoNulls(IEnumerable<string?> src) => src is not null && !src.Any(ns => ns is null);","tryCatchPattern":"try { new XamlDirective(namespaces, name, type, invoker, loc); }\ncatch (ArgumentException ex) when (ex.ParamName == \"xamlNamespaces\") { /* sanitize list and retry */ }","preventionTips":["Coalesce null namespace strings to string.Empty at the data source","Use string[] with nullable annotations disabled at boundaries","Validate collections built from parsed/configured strings before passing on"],"tags":["wpf","system-xaml","argument-exception","null-in-collection"],"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"}