{"record":{"id":"754b818b49ff62d5","repo":"dotnet/wpf","slug":"sr-namespacedeclarationnamespacecannotbenull","errorCode":null,"errorMessage":"SR.NamespaceDeclarationNamespaceCannotBeNull","messagePattern":"SR\\.NamespaceDeclarationNamespaceCannotBeNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs","lineNumber":903,"sourceCode":"                        Logic_DoAssignmentToParentCollection(_context);\n                        _context.PopScope();\n                    }\n                }\n            }\n        }\n\n        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)\n        {\n            ThrowIfDisposed();\n            ArgumentNullException.ThrowIfNull(namespaceDeclaration);\n            if (namespaceDeclaration.Prefix is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull);\n            }\n\n            if (namespaceDeclaration.Namespace is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNull);\n            }\n\n            // Deferring Checking\n            //\n            _deferringWriter.WriteNamespace(namespaceDeclaration);\n            if (_deferringWriter.Handled)\n            {\n                return;\n            }\n\n            // Error Checking\n            //\n            if (_nextNodeMustBeEndMember)\n            {\n                string err = SR.ValueMustBeFollowedByEndMember;\n                throw _context.WithLineInfo(new XamlObjectWriterException(err));\n            }\n","sourceCodeStart":885,"sourceCodeEnd":921,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs#L885-L921","documentation":"ArgumentException thrown by XamlObjectWriter.WriteNamespace when the supplied NamespaceDeclaration has a null Namespace (the namespace URI). Along with the prefix, the URI is mandatory for any namespace declaration the writer accepts.","triggerScenarios":"Calling WriteNamespace(new NamespaceDeclaration(somePrefix, null)) — the namespace URI argument is null.","commonSituations":"Custom readers that lose the URI when collapsing duplicate xmlns declarations; constructing namespace declarations from parsed attributes where the value attribute was missing; generated node streams in tooling.","solutions":["Supply a valid namespace URI string when constructing the NamespaceDeclaration.","Skip writing the declaration entirely if its URI is null rather than forwarding it.","Guard before the call: if (ns.Namespace != null) writer.WriteNamespace(ns);"],"exampleFix":"// before\nwriter.WriteNamespace(new NamespaceDeclaration(\"x\", null)); // throws\n// after\nif (ns.Namespace != null)\n    writer.WriteNamespace(new NamespaceDeclaration(ns.Prefix ?? string.Empty, ns.Namespace));","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(ns.Namespace))\n    throw new InvalidOperationException(\"NamespaceDeclaration requires a namespace URI.\"); // or skip the declaration","typeGuard":"static bool HasNamespace(NamespaceDeclaration ns) => ns?.Namespace != null;","tryCatchPattern":"try { writer.WriteNamespace(ns); }\ncatch (ArgumentException) { LogSkippedDeclaration(ns); } // drop invalid declaration","preventionTips":["Never emit namespace declarations with missing URIs; drop or default them upstream.","Validate xmlns attribute values when building declarations from parsed XML.","Null-check Namespace before WriteNamespace.","Cover xmlns-edge cases in node-stream producer tests."],"tags":["xaml","argumentexception","namespace","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"}