dotnet/maui · error · ArgumentNullException

throw new ArgumentNullException(nameof(type));

Error message

throw new ArgumentNullException(nameof(type));

What it means

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.

Source

Thrown at src/Controls/src/Core/ElementTemplate.cs:28

	public class ElementTemplate : IElementDefinition
	{
		List<Action<object, ResourcesChangedEventArgs>> _changeHandlers;
		Element _parent;
		bool _canRecycle; // aka IsDeclarative

		[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
		readonly Type _type;

		internal ElementTemplate()
		{
		}

		internal ElementTemplate(
			[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
			: this()
		{
			if (type == null)
				throw new ArgumentNullException(nameof(type));

			_canRecycle = true;
			_type = type;

			LoadTemplate = () => Activator.CreateInstance(type);
		}

		internal ElementTemplate(Func<object> loadTemplate) : this() => LoadTemplate = loadTemplate ?? throw new ArgumentNullException(nameof(loadTemplate));

		public Func<object> LoadTemplate { get; set; }

		void IElementDefinition.AddResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)
		{
			_changeHandlers = _changeHandlers ?? new List<Action<object, ResourcesChangedEventArgs>>(1);
			_changeHandlers.Add(onchanged);
		}

		internal bool CanRecycle => _canRecycle;

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Null-check the Type before passing it to the ElementTemplate constructor.
  2. Ensure the assembly containing the type is loaded and the type name is correct.
  3. Provide a fallback template type for the null case.

Example fix

// before
var template = new DataTemplate(ResolvedType); // throws if ResolvedType is null
// after
if (ResolvedType == null) throw new InvalidOperationException("Type not found");
var template = new DataTemplate(ResolvedType);
Defensive patterns

Strategy: validation

Validate before calling

if (type == null) throw new ArgumentNullException(nameof(type));
var template = new DataTemplate(type);

Type guard

static bool IsValidTemplateType(Type t) => t != null &&
    typeof(Element).IsAssignableFrom(t);

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/b7c173b82a097886. Report an issue: GitHub.