unoplatform/uno · error · ArgumentNullException

writer

Error message

writer

What it means

XamlServices.Save(XamlWriter, object) throws ArgumentNullException('writer') when the XamlWriter argument is null. Save wraps the writer with a XamlObjectReader and calls Transform, so it cannot proceed without a destination writer.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlServices.cs:100

			using (var xw = XmlWriter.Create (stream, new XmlWriterSettings () { OmitXmlDeclaration = true, Indent = true }))
				Save (xw, instance);
		}

		public static void Save (TextWriter writer, object instance)
		{
			using (var xw = XmlWriter.Create (writer, new XmlWriterSettings () { OmitXmlDeclaration = true, Indent = true }))
				Save (xw, instance);
		}

		public static void Save (XmlWriter writer, object instance)
		{
			Save (new XamlXmlWriter (writer, new XamlSchemaContext ()), instance);
		}

		public static void Save (XamlWriter writer, object instance)
		{
			if (writer == null)
				throw new ArgumentNullException ("writer");
			var r = new XamlObjectReader (instance, writer.SchemaContext);
			Transform (r, writer);
		}

		public static void Transform (XamlReader xamlReader, XamlWriter xamlWriter)
		{
			Transform (xamlReader, xamlWriter, true);
		}

		public static void Transform (XamlReader xamlReader, XamlWriter xamlWriter, bool closeWriter)
		{
			if (xamlReader == null)
				throw new ArgumentNullException ("xamlReader");
			if (xamlWriter == null)
				throw new ArgumentNullException ("xamlWriter");

			if (xamlReader.NodeType == XamlNodeType.None)
				xamlReader.Read ();

View on GitHub (pinned to 0418340488)

Solutions

  1. Null-check the XamlWriter before calling Save and surface a clear error or skip serialization.
  2. Ensure the writer factory never returns null — if construction fails it should throw rather than return null.
  3. Prefer the Save(TextWriter)/Save(XmlWriter)/Save(string) overloads which create the writer internally when you only need standard output.

Example fix

// before
XamlServices.Save((XamlWriter) writer, instance);

// after
if (writer == null) throw new ArgumentNullException(nameof(writer));
XamlServices.Save(writer, instance);
Defensive patterns

Strategy: validation

Validate before calling

if (writer == null) throw new ArgumentNullException(nameof(writer));
XamlServices.Save(writer, instance);

Prevention

When it happens

Trigger: Passing a null XamlWriter to Save, commonly from a code path where the writer was conditionally created (e.g. only when a file could be opened) but Save is called unconditionally.

Common situations: Wrapping Save in a using/dispose block where the writer creation failed silently; refactoring that removed writer instantiation; tests passing null to verify argument validation.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/eda066b5284255da. Report an issue: GitHub.