dotnet/maui · error · ArgumentNullException

self

Error message

self

What it means

VisualElementExtensions.GetOrCreateRenderer is an extension method that throws ArgumentNullException when self (the VisualElement) is null. It retrieves or creates a platform renderer for the element via Platform.GetRenderer/CreateRenderer.

Source

Thrown at src/Compatibility/Core/src/Windows/VisualElementExtensions.cs:13

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

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

			IVisualElementRenderer renderer = Platform.GetRenderer(self);
			if (renderer == null)
			{
				renderer = Platform.CreateRenderer(self);
				Platform.SetRenderer(self, renderer);
			}

			return renderer;
		}

		internal static void Cleanup(this VisualElement self)
		{
			if (self == null)
				throw new ArgumentNullException("self");

			IVisualElementRenderer renderer = Platform.GetRenderer(self);

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Null-check the VisualElement before calling GetOrCreateRenderer()
  2. Ensure the element is constructed and not disposed before renderer access
  3. Use defensive programming in lifecycle-critical code paths

Example fix

// before
IVisualElementRenderer renderer = myView.GetOrCreateRenderer(); // throws if myView is null

// after
if (myView == null)
    throw new InvalidOperationException(nameof(myView) + " must not be null");
IVisualElementRenderer renderer = myView.GetOrCreateRenderer();
Defensive patterns

Strategy: validation

Validate before calling

// Before calling GetOrCreateRenderer, null-check
if (self == null)
    throw new ArgumentNullException(nameof(self));
var renderer = self.GetOrCreateRenderer();

Prevention

When it happens

Trigger: Calling GetOrCreateRenderer() on a null VisualElement reference — e.g. a page or view that was never initialized, disposed, or nulled out before the call.

Common situations: View reference set to null during lifecycle teardown but renderer accessed afterward; page property not yet initialized when accessed; null-coalescing chain that unexpectedly yields null.

Related errors


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