dotnet/maui · error · ArgumentNullException

page

Error message

page

What it means

In the same InsertPageBefore method, 'page' (the page to insert) must be non-null because it is passed to CreateViewControllerForPage(page) (line 573) which builds its UIViewController. A null page cannot be turned into a controller.

Source

Thrown at src/Compatibility/Core/src/iOS/Renderers/NavigationRenderer.cs:571

		[PortHandler]
		void UpdateUseLargeTitles()
		{
			if (Forms.IsiOS11OrNewer && NavPage != null)
				NavigationBar.PrefersLargeTitles = NavPage.OnThisPlatform().PrefersLargeTitles();
		}

		[PortHandler]
		void UpdateTranslucent()
		{
			NavigationBar.Translucent = NavPage.OnThisPlatform().IsNavigationBarTranslucent();
		}

		void InsertPageBefore(Page page, Page before)
		{
			if (before == null)
				throw new ArgumentNullException("before");
			if (page == null)
				throw new ArgumentNullException("page");

			var pageContainer = CreateViewControllerForPage(page);
			var target = Platform.GetRenderer(before).ViewController.ParentViewController;
			ViewControllers = ViewControllers.Insert(ViewControllers.IndexOf(target), pageContainer);
		}

		void OnInsertPageBeforeRequested(object sender, NavigationRequestedEventArgs e)
		{
			InsertPageBefore(e.Page, e.BeforePage);
		}

		void OnPopRequested(object sender, NavigationRequestedEventArgs e)
		{
			e.Task = PopViewAsync(e.Page, e.Animated);
		}

		void OnPopToRootRequested(object sender, NavigationRequestedEventArgs e)
		{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Construct the page instance before calling InsertPageBefore and ensure it is non-null.
  2. Validate the page against null in view-model/navigation-service code.
  3. Use a null-coalescing factory so a default page is always produced.

Example fix

// before
Navigation.InsertPageBefore(CreatePageOrDefault(), before);

// after
var page = CreatePageOrDefault();
if (page != null)
    Navigation.InsertPageBefore(page, before);
Defensive patterns

Strategy: validation

Validate before calling

var page = CreatePage();
if (page == null) throw new ArgumentNullException(nameof(page));
Navigation.InsertPageBefore(page, before);

Type guard

static bool IsPage(object o) => o is Page;

Prevention

When it happens

Trigger: Calling Navigation.InsertPageBefore(null, before) - the page to insert was not constructed or was nulled out.

Common situations: Passing a factory result that returned null; binding-driven navigation where the bound page is null; conditional page creation that did not assign.

Related errors


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