{"record":{"id":"02e715ff7e8ab4c5","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-writer","errorCode":null,"errorMessage":"ArgumentNullException(nameof(writer))","messagePattern":"ArgumentNullException\\(nameof\\(writer\\)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlBrushSerializer.cs","lineNumber":97,"sourceCode":"        /// </summary>\n        /// <remarks>\n        /// This is called ONLY from the Parser and is not a general public method. \n        /// </remarks>\n        public override object ConvertCustomBinaryToObject(\n            BinaryReader reader)\n        {\n            // ********* VERY IMPORTANT NOTE *****************\n            // If this method is changed, then BamlPropertyCustomRecord.GetCustomValue() needs\n            // to be correspondingly changed as well\n            // ********* VERY IMPORTANT NOTE *****************\n            return SolidColorBrush.DeserializeFrom(reader);\n        }\n#else\n        private static bool SerializeOn(BinaryWriter writer, string stringValue)\n        {\n            if (writer == null)\n            {\n                throw new ArgumentNullException(nameof(writer));\n            }\n\n            KnownColor knownColor = KnownColors.ColorStringToKnownColor(stringValue);\n#if !PBTCOMPILER\n            // ***************** NOTE *****************\n            // This section under #if !PBTCOMPILER is not needed in XamlBrushSerializer.cs\n            // because XamlBrushSerializer.SerializeOn() is only compiled when PBTCOMPILER is set. \n            // If this code were tried to be compiled in XamlBrushSerializer.cs, it wouldn't compile\n            // becuase of missing definition of s_knownSolidColorBrushStringCache. \n            // This code is added in XamlBrushSerializer.cs nevertheless for maintaining consistency in the codebase\n            // between XamlBrushSerializer.SerializeOn() and SolidColorBrush.SerializeOn().\n            // ***************** NOTE *****************\n            lock (s_knownSolidColorBrushStringCache)\n            {\n                SolidColorBrush scb; \n                if (s_knownSolidColorBrushStringCache.ContainsValue(stringValue))\n                {\n                    knownColor = KnownColors.ArgbStringToKnownColor(stringValue);","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlBrushSerializer.cs#L79-L115","documentation":"XamlBrushSerializer.SerializeOn validates its BinaryWriter argument and throws ArgumentNullException when it is null. It is reached via ConvertStringToCustomBinary during brush/color serialization, so a null writer indicates the caller misconfigured the serialization stream.","triggerScenarios":"Calling XamlBrushSerializer.ConvertStringToCustomBinary (or the serializer pipeline) with a null BinaryWriter, e.g. supplying a null stream/StreamContext to the XAML serializer infrastructure.","commonSituations":"Custom XAML serialization tooling invoking the brush serializer directly; a null stream passed to XamlWriter/serializer service plumbing; reflection-based calls bypassing the writer setup.","solutions":["Pass a valid, non-null BinaryWriter backed by an open stream to the serializer","Ensure the surrounding XAML serialization service created its stream before conversion","For custom code, guard against null writer before calling the serializer"],"exampleFix":"// before\nXamlBrushSerializer.SerializeOn(null, \"#FFFF0000\");\n// after\nusing var ms = new MemoryStream();\nusing var writer = new BinaryWriter(ms);\nXamlBrushSerializer.SerializeOn(writer, \"#FFFF0000\");","handlingStrategy":"type-guard","validationCode":"if (writer == null) throw new ArgumentNullException(nameof(writer));","typeGuard":"bool HasWriter(BinaryWriter w) => w != null;","tryCatchPattern":"try { XamlBrushSerializer.SerializeOn(writer, value); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"writer\") { /* initialize the stream/writer first */ }","preventionTips":["Always create the BinaryWriter (and its stream) before serializer calls","Dispose streams only after serialization completes","Null-check arguments in custom serializer plumbing"],"tags":["wpf","xaml","serialization","null-argument"],"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"}