dotnet/maui · error · ArgumentNullException

self

Error message

self

What it means

VisualElementExtensions.GetRenderer(this VisualElement self) throws ArgumentNullException(nameof(self)) when called on a null VisualElement. The extension method exists to retrieve the platform renderer and cannot operate without an element.

Source

Thrown at src/Compatibility/Core/src/iOS/Extensions/VisualElementExtensions.cs:13

using System;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS
{
	[System.Obsolete]
	public static class VisualElementExtensions
	{
		public static IVisualElementRenderer GetRenderer(this VisualElement self)
		{
			if (self == null)
				throw new ArgumentNullException(nameof(self));

			return Platform.GetRenderer(self);
		}
		internal static bool UseLegacyColorManagement<T>(this T element) where T : VisualElement, IElementConfiguration<T>
		{
			// Determine whether we're letting the VSM handle the colors or doing it the old way
			// or disabling the legacy color management and doing it the old-old (pre 2.0) way
			return !element.HasVisualStateGroups()
					&& element.OnThisPlatform().GetIsLegacyColorModeEnabled();
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Null-check the VisualElement before calling GetRenderer().
  2. Use Platform.GetRenderer directly if you want null-tolerant behavior.
  3. Audit custom renderer code paths that reach GetRenderer during detachment/disposal.

Example fix

// before
var r = element.GetRenderer();
// after
if (element != null)
    var r = element.GetRenderer();
Defensive patterns

Strategy: validation

Validate before calling

if (self == null) return null;
return self.GetRenderer();

Type guard

static bool HasRenderer(VisualElement e) => e != null && Platform.GetRenderer(e) != null;

Prevention

When it happens

Trigger: Invoking GetRenderer() on a null VisualElement reference; common when a renderer/accessory accesses an Element that has not been set or has been cleared.

Common situations: Custom renderers calling this.GetRenderer() where `this` context has a null element; layout code operating on a recycled element; chaining extension methods on possibly-null visual elements without guarding.

Related errors


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