unoplatform/uno · error · ArgumentNullException
instance
Error message
instance
What it means
Thrown by XamlTypeInvoker.AddToCollection when the `instance` argument is null. AddToCollection reflects an `Add` method on the instance and invokes it, so a null collection has no type to reflect and nothing to add to.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs:74
void ThrowIfUnknown ()
{
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 ());View on GitHub (pinned to 0418340488)
Solutions
- Ensure the collection instance is resolved and non-null before calling AddToCollection (e.g. instantiate the collection in the property getter if null).
- In a custom object writer, check that the current object on the stack is non-null at the point of adding collection items.
- Guard: `if (instance == null) { ...initialize or throw with context... }` before the call.
Example fix
// before type.Invoker.AddToCollection(collection, item); // collection may be null // after collection ??= (IList)Activator.CreateInstance(collectionType); type.Invoker.AddToCollection(collection, item);
Defensive patterns
Strategy: validation
Validate before calling
// before AddToCollection
if (instance == null) {
throw new InvalidOperationException("Collection instance is null; initialize it before adding items.");
} Prevention
- Initialize collection properties eagerly (field initializer or getter).
- In custom writers, verify the current object on the stack is non-null before adding children.
When it happens
Trigger: Calling AddToCollection(null, item) directly, or a XAML object-writer path where the collection object (from GetItems or a property value) was null when processing a collection-member child item.
Common situations: XAML where a collection property is lazily initialized but the writer attempted to add children before the collection was created; custom writers that resolve the parent collection to null; null-propagation from a failed property get.
Related errors
- item
- type
- Non-collection type '{0}' does not support this operation
- The collection type '{0}' does not have 'Add' method
- Attempt to set value from read-only property {0}
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/6856d26deaef6c5d.
Report an issue: GitHub.