unoplatform/uno · error · ArgumentNullException

item

Error message

item

What it means

Thrown by XamlTypeInvoker.AddToCollection when the `item` argument is null. The method reflects an Add(T) overload based on item.GetType(), so a null item both breaks type resolution and is usually a semantic error in XAML (XAML items are objects with identity).

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs:76

		{
			if (type == null || type.UnderlyingType == null)
				throw new NotSupportedException (String.Format (CultureInfo.InvariantCulture, "Current operation is valid only when the underlying type on a XamlType is known, but it is unknown for '{0}'", type));
		}

		public EventHandler<XamlSetMarkupExtensionEventArgs> SetMarkupExtensionHandler {
			get { return type == null ? null : type.SetMarkupExtensionHandler; }
		}

		public EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler {
			get { return type == null ? null : type.SetTypeConverterHandler; }
		}

		public virtual void AddToCollection (object instance, object item)
		{
			if (instance == null)
				throw new ArgumentNullException ("instance");
			if (item == null)
				throw new ArgumentNullException ("item");

			var ct = instance.GetType ();
			var xct = type == null ? null : type.SchemaContext.GetXamlType (ct);
			MethodInfo mi = null;

			// FIXME: this method lookup should be mostly based on GetAddMethod(). At least iface method lookup must be done there.
			if (type != null && type.UnderlyingType != null) {
				if (!xct.IsCollection) // not sure why this check is done only when UnderlyingType exists...
					throw new NotSupportedException (String.Format (CultureInfo.InvariantCulture, "Non-collection type '{0}' does not support this operation", xct));
				if (ct.IsAssignableFrom (type.UnderlyingType))
					mi = GetAddMethod (type.SchemaContext.GetXamlType (item.GetType ()));
			}

			if (mi == null) {
				if (ct.IsGenericType) {
					mi = ct.GetMethod ("Add", ct.GetGenericArguments ());
					if (mi == null)
						mi = LookupAddMethod (ct, typeof (ICollection<>).MakeGenericType (ct.GetGenericArguments ()));

View on GitHub (pinned to 0418340488)

Solutions

  1. Do not add null items to XAML collections — filter nulls out before calling AddToCollection.
  2. If x:Null is intentional, handle it at the writer level (skip the add) rather than passing null through.
  3. Ensure MarkupExtensions return a real instance.

Example fix

// before
type.Invoker.AddToCollection(collection, item); // item may be null

// after
if (item != null) {
    type.Invoker.AddToCollection(collection, item);
}
Defensive patterns

Strategy: validation

Validate before calling

// before AddToCollection
if (item == null) return; // skip null items

Prevention

When it happens

Trigger: Calling AddToCollection(instance, null) directly, or a XAML writer adding a child that resolved to null (e.g. a null MarkupExtension result, or a property whose value computed to null being added to a collection).

Common situations: Markup extensions returning null that get added to a collection; x:Null used in a position where an item is expected to be added; bugs in object construction that yield null items.

Related errors


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