{"record":{"id":"b7c173b82a097886","repo":"dotnet/maui","slug":"throw-new-argumentnullexception-nameof-type","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(type));","messagePattern":"throw new ArgumentNullException\\(nameof\\(type\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/ElementTemplate.cs","lineNumber":28,"sourceCode":"\tpublic class ElementTemplate : IElementDefinition\n\t{\n\t\tList<Action<object, ResourcesChangedEventArgs>> _changeHandlers;\n\t\tElement _parent;\n\t\tbool _canRecycle; // aka IsDeclarative\n\n\t\t[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]\n\t\treadonly Type _type;\n\n\t\tinternal ElementTemplate()\n\t\t{\n\t\t}\n\n\t\tinternal ElementTemplate(\n\t\t\t[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)\n\t\t\t: this()\n\t\t{\n\t\t\tif (type == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(type));\n\n\t\t\t_canRecycle = true;\n\t\t\t_type = type;\n\n\t\t\tLoadTemplate = () => Activator.CreateInstance(type);\n\t\t}\n\n\t\tinternal ElementTemplate(Func<object> loadTemplate) : this() => LoadTemplate = loadTemplate ?? throw new ArgumentNullException(nameof(loadTemplate));\n\n\t\tpublic Func<object> LoadTemplate { get; set; }\n\n\t\tvoid IElementDefinition.AddResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)\n\t\t{\n\t\t\t_changeHandlers = _changeHandlers ?? new List<Action<object, ResourcesChangedEventArgs>>(1);\n\t\t\t_changeHandlers.Add(onchanged);\n\t\t}\n\n\t\tinternal bool CanRecycle => _canRecycle;","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/ElementTemplate.cs#L10-L46","documentation":"Thrown by the ElementTemplate(Type) constructor when the type argument is null. ElementTemplate is the base for DataTemplate and ControlTemplate; the Type-based constructor wires up LoadTemplate to Activator.CreateInstance(type) and enables recycling (CanRecycle). A null type is rejected immediately because it cannot be instantiated.","triggerScenarios":"Constructing new DataTemplate((Type)null) or new ControlTemplate((Type)null). This can happen when resolving a type via reflection/assembly scanning that returns null, or when passing a typeof() expression that resolves to null in conditional code.","commonSituations":"Programmatic DataTemplate/ControlTemplate creation from a type resolved at runtime that is missing; assembly scanning or type-from-name lookup returning null; conditional template selection with a null branch.","solutions":["Null-check the Type before passing it to the ElementTemplate constructor.","Ensure the assembly containing the type is loaded and the type name is correct.","Provide a fallback template type for the null case."],"exampleFix":"// before\nvar template = new DataTemplate(ResolvedType); // throws if ResolvedType is null\n// after\nif (ResolvedType == null) throw new InvalidOperationException(\"Type not found\");\nvar template = new DataTemplate(ResolvedType);","handlingStrategy":"validation","validationCode":"if (type == null) throw new ArgumentNullException(nameof(type));\nvar template = new DataTemplate(type);","typeGuard":"static bool IsValidTemplateType(Type t) => t != null &&\n    typeof(Element).IsAssignableFrom(t);","tryCatchPattern":null,"preventionTips":["Null-check the Type before passing it to the DataTemplate/ControlTemplate constructor.","Verify the type is found when resolving via reflection/assembly scanning.","Provide a fallback type for the null-resolution case."],"tags":["maui","argumentnull","datatemplate","controltemplate","reflection"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}