dotnet/maui · error · ArgumentNullException

newElement

Error message

newElement

What it means

Tizen's VisualElementRenderer.SetElement throws ArgumentNullException(nameof(newElement)) when a null element is passed. SetElement binds a platform renderer to a Xamarin/Maui VisualElement and drives the renderer swap lifecycle, so a null element aborts it before any native view is attached.

Source

Thrown at src/Compatibility/Core/src/Tizen/Renderers/VisualElementRenderer.cs:169

					measured = nativeViewMeasurable.Measure(availableWidth, availableHeight).ToDP();
				}
				else
				{
					measured = Measure(widthConstraint, heightConstraint);
				}

				return new SizeRequest(measured, MinimumSize());
			}
		}

		/// <summary>
		/// Sets the element associated with this renderer.
		/// </summary>
		public void SetElement(TElement newElement)
		{
			if (newElement == null)
			{
				throw new ArgumentNullException(nameof(newElement));
			}

			TElement oldElement = Element;

			Element = newElement;

			if (oldElement != null)
			{
				Platform.SetRenderer(oldElement, null);
			}

			// send notification
			OnElementChanged(new ElementChangedEventArgs<TElement>(oldElement, newElement));

			// store renderer for the new element
			Platform.SetRenderer(newElement, this);

			// add children

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Null-check the element before calling SetElement: if (element == null) return;
  2. Ensure the Forms Element is fully constructed (its RealParent is set) before invoking the renderer swap.
  3. If reusing a renderer, call Dispose/null its Element through Platform.SetRenderer(old, null) first, then SetElement(newElement) with a non-null instance.
  4. Audit custom renderers overriding SetElement to ensure they forward a non-null TElement to base.

Example fix

// before
var renderer = new CustomRenderer();
renderer.SetElement(element); // element may be null

// after
if (element != null)
{
    renderer.SetElement(element);
}
Defensive patterns

Strategy: validation

Validate before calling

if (newElement == null) return; // or throw a domain-specific error
renderer.SetElement(newElement);

Type guard

static bool IsValidElement(VisualElement e) => e != null && e.RealParent != null;

Try / catch

try { renderer.SetElement(element); }
catch (ArgumentNullException ex) when (ex.ParamName == nameof(element))
{
    // log and recover; element was unexpectedly null
}

Prevention

When it happens

Trigger: Calling SetElement(null) directly on a VisualElementRenderer<TElement>, or the platform's renderer-recycling path (Platform.SetRenderer / registrar) handing null when swapping renderers during page navigation or element reparenting.

Common situations: Custom Tizen renderer that calls SetElement before the Element is constructed; page recycled by the platform with a stale reference; binding context cleared mid-lifecycle leaving Element null; manual renderer instantiation that skips element wiring.

Related errors


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