unoplatform/uno · error · ArgumentNullException

xamlReader

Error message

xamlReader

What it means

XamlServices.Load(XamlReader) throws ArgumentNullException('xamlReader') when the caller passes a null XamlReader. The method needs a reader to construct a XamlObjectWriter against the reader's SchemaContext and then drive Transform, so a null reader has no schema context to use.

Source

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

		public static Object Load (Stream stream)
		{
			return Load (new XamlXmlReader (stream));
		}

		public static Object Load (TextReader textReader)
		{
			return Load (new XamlXmlReader (textReader));
		}

		public static Object Load (XmlReader xmlReader)
		{
			return Load (new XamlXmlReader (xmlReader));
		}

		public static Object Load (XamlReader xamlReader)
		{
			if (xamlReader == null)
				throw new ArgumentNullException ("xamlReader");
			var w = new XamlObjectWriter (xamlReader.SchemaContext);
			Transform (xamlReader, w);
			return w.Result;
		}

		public static Object Parse (string xaml)
		{
			return Load (new StringReader (xaml));
		}

		public static string Save (object instance)
		{
			var sw = new StringWriter ();
			Save (sw, instance);
			return sw.ToString ();
		}

		public static void Save (string fileName, object instance)

View on GitHub (pinned to 0418340488)

Solutions

  1. Null-check the XamlReader before calling Load and handle the null case explicitly (return early or throw a domain-specific exception).
  2. Ensure the factory/builder that produces the XamlReader never returns null — return a XamlXmlReader over an empty stream instead, or throw at the source.
  3. If the input is a string/XmlReader, prefer the typed overloads Load(string)/Load(XmlReader) which construct the reader internally.

Example fix

// before
XamlServices.Load((XamlReader) reader);

// after
if (reader == null) throw new ArgumentNullException(nameof(reader));
XamlServices.Load(reader);
Defensive patterns

Strategy: validation

Validate before calling

if (xamlReader == null) throw new ArgumentNullException(nameof(xamlReader));
XamlServices.Load(xamlReader);

Prevention

When it happens

Trigger: Calling XamlServices.Load((XamlReader)null), or passing a variable that was conditionally assigned null (e.g. a factory method returned null because the input stream was empty). Also when a wrapper returns null instead of a XamlXmlReader.

Common situations: Defensive code paths that create a reader only when a condition holds but unconditionally call Load; refactoring that changes a method to return null on error; unit tests feeding null to exercise error handling.

Related errors


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