{"record":{"id":"6856d26deaef6c5d","repo":"unoplatform/uno","slug":"instance-6856d2","errorCode":null,"errorMessage":"instance","messagePattern":"instance","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs","lineNumber":74,"sourceCode":"\n\t\tvoid ThrowIfUnknown ()\n\t\t{\n\t\t\tif (type == null || type.UnderlyingType == null)\n\t\t\t\tthrow 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));\n\t\t}\n\n\t\tpublic EventHandler<XamlSetMarkupExtensionEventArgs> SetMarkupExtensionHandler {\n\t\t\tget { return type == null ? null : type.SetMarkupExtensionHandler; }\n\t\t}\n\n\t\tpublic EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler {\n\t\t\tget { return type == null ? null : type.SetTypeConverterHandler; }\n\t\t}\n\n\t\tpublic virtual void AddToCollection (object instance, object item)\n\t\t{\n\t\t\tif (instance == null)\n\t\t\t\tthrow new ArgumentNullException (\"instance\");\n\t\t\tif (item == null)\n\t\t\t\tthrow new ArgumentNullException (\"item\");\n\n\t\t\tvar ct = instance.GetType ();\n\t\t\tvar xct = type == null ? null : type.SchemaContext.GetXamlType (ct);\n\t\t\tMethodInfo mi = null;\n\n\t\t\t// FIXME: this method lookup should be mostly based on GetAddMethod(). At least iface method lookup must be done there.\n\t\t\tif (type != null && type.UnderlyingType != null) {\n\t\t\t\tif (!xct.IsCollection) // not sure why this check is done only when UnderlyingType exists...\n\t\t\t\t\tthrow new NotSupportedException (String.Format (CultureInfo.InvariantCulture, \"Non-collection type '{0}' does not support this operation\", xct));\n\t\t\t\tif (ct.IsAssignableFrom (type.UnderlyingType))\n\t\t\t\t\tmi = GetAddMethod (type.SchemaContext.GetXamlType (item.GetType ()));\n\t\t\t}\n\n\t\t\tif (mi == null) {\n\t\t\t\tif (ct.IsGenericType) {\n\t\t\t\t\tmi = ct.GetMethod (\"Add\", ct.GetGenericArguments ());","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs#L56-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntype.Invoker.AddToCollection(collection, item); // collection may be null\n\n// after\ncollection ??= (IList)Activator.CreateInstance(collectionType);\ntype.Invoker.AddToCollection(collection, item);","handlingStrategy":"validation","validationCode":"// before AddToCollection\nif (instance == null) {\n    throw new InvalidOperationException(\"Collection instance is null; initialize it before adding items.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize collection properties eagerly (field initializer or getter).","In custom writers, verify the current object on the stack is non-null before adding children."],"tags":["xaml","schema","invoker","collection","argument-validation"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}