dotnet/maui · error · ArgumentNullException

mainRenderer

Error message

mainRenderer

What it means

ModalPageTracker's constructor requires a non-null NSViewController (the main renderer) because it immediately accesses _renderer.View.WantsLayer and uses the renderer to present modals. A null renderer means there is no host view to layer and present against, so it throws ArgumentNullException(nameof(mainRenderer)) (message 'mainRenderer').

Source

Thrown at src/Compatibility/Core/src/MacOS/ModalPageTracker.cs:18

using System;
using System.Threading.Tasks;
using System.Linq;
using AppKit;
using System.Collections.Generic;

namespace Microsoft.Maui.Controls.Compatibility.Platform.MacOS
{
	internal class ModalPageTracker : IDisposable
	{
		NSViewController _renderer;
		List<Page> _modals;
		bool _disposed;

		public ModalPageTracker(NSViewController mainRenderer)
		{
			if (mainRenderer == null)
				throw new ArgumentNullException(nameof(mainRenderer));
			_renderer = mainRenderer;
			_renderer.View.WantsLayer = true;
			_modals = new List<Page>();
		}

		public List<Page> ModalStack => _modals;

		public Task PushAsync(Page modal, bool animated)
		{
			_modals.Add(modal);
			modal.DescendantRemoved += HandleChildRemoved;
			Platform.NativeToolbarTracker.TryHide(modal as NavigationPage);
			return PresentModalAsync(modal, animated);
		}

		public Task<Page> PopAsync(bool animated)
		{
			var modal = _modals.LastOrDefault();

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure Platform is constructed with a valid NSViewController/renderer before any modal navigation occurs.
  2. In tests, pass a real or mock NSViewController (with a non-null View) to ModalPageTracker.
  3. Trace why the main renderer is null — it usually indicates an upstream Platform initialization problem.

Example fix

// before
var tracker = new ModalPageTracker(null); // throws

// after
var vc = new NSViewController { View = new NSView() };
var tracker = new ModalPageTracker(vc);
Defensive patterns

Strategy: validation

Validate before calling

if (mainRenderer == null) throw new ArgumentNullException(nameof(mainRenderer));
var tracker = new ModalPageTracker(mainRenderer);

Type guard

static bool IsValidRenderer(NSViewController vc) => vc != null && vc.View != null;

Prevention

When it happens

Trigger: Constructing ModalPageTracker with null — typically inside Platform when the platform renderer/view controller is null. An internal failure to create the main renderer surfaces here. Unlikely to be reached by user code directly but reflects a malformed Platform setup.

Common situations: Custom Platform construction on macOS without a backing NSViewController. Test setup that instantiates ModalPageTracker with null. A failure earlier in Platform setup that left the renderer null.

Related errors


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