dotnet/maui · error · ArgumentNullException

view

Error message

view

What it means

TemplateHelpers.CreateRenderer throws ArgumentNullException(nameof(view)) when the View passed in is null. The helper cannot create or attach a renderer for a null element, so it fails fast rather than producing a null renderer that propagates a NullReferenceException later in the layout pipeline.

Source

Thrown at src/Compatibility/Core/src/iOS/CollectionView/TemplateHelpers.cs:15

using System;
using Microsoft.Maui.Controls.Internals;
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS
{
	[Obsolete]
	internal static class TemplateHelpers
	{
		public static IVisualElementRenderer CreateRenderer(View view)
		{
			if (view == null)
			{
				throw new ArgumentNullException(nameof(view));
			}

			Platform.GetRenderer(view)?.DisposeRendererAndChildren();
			var renderer = Platform.CreateRenderer(view);
			Platform.SetRenderer(view, renderer);

			renderer.NativeView.Bounds = view.Bounds.ToRectangleF();

			return renderer;
		}

		public static (UIView NativeView, VisualElement FormsElement) RealizeView(object view, DataTemplate viewTemplate, ItemsView itemsView)
		{
			if (viewTemplate != null)
			{
				// Run this through the extension method in case it's really a DataTemplateSelector
				viewTemplate = viewTemplate.SelectDataTemplate(view, itemsView);

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Audit DataTemplateSelector implementations so every branch returns a non-null DataTemplate.
  2. Ensure ItemTemplate / DataTemplate factories always return a non-null View.
  3. Add a null check in the caller before invoking CreateRenderer.

Example fix

// before
var r = TemplateHelpers.CreateRenderer(view);
// after
if (view == null)
    throw new InvalidOperationException("View returned by template was null. Check selector/template.");
var r = TemplateHelpers.CreateRenderer(view);
Defensive patterns

Strategy: validation

Validate before calling

if (view == null)
    throw new InvalidOperationException("DataTemplate returned a null View. Review selector/template.");
var renderer = TemplateHelpers.CreateRenderer(view);

Type guard

static bool IsValidView(View v) => v != null;

Prevention

When it happens

Trigger: Calling TemplateHelpers.CreateRenderer(null); typically reached when a DataTemplate returns null (e.g. a DataTemplateSelector selects a missing template) or when ItemsView.ItemTemplate binds to a null View.

Common situations: A DataTemplateSelector returning null for an unhandled case; an ItemTemplate where the type-creating factory returns null; x:Null set on a template; F# code constructing Views lazily and passing null on the first call.

Related errors


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