dotnet/maui · error · ArgumentNullException

Shell Content Page is Null

Error message

Shell Content Page is Null

What it means

Thrown by the Android ShellSectionRenderer in UpdateCurrentItem when GetOrCreateContent() returns null for a ShellContent. The cast `(IShellContentController)content).GetOrCreateContent()` should always produce a Page (lazily creating it from the ContentTemplate), so a null result indicates the ContentTemplate failed to instantiate a page, or the ShellContent was misconfigured. The exception is ArgumentNullException with the parameter name 'page'.

Source

Thrown at src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellSectionRenderer.cs:42

{
	public class ShellSectionRenderer : Fragment, IShellSectionRenderer//, ViewPager.IOnPageChangeListener
		, AView.IOnClickListener, IShellObservableFragment, IAppearanceObserver, TabLayoutMediator.ITabConfigurationStrategy
	{
		#region ITabConfigurationStrategy

		void TabLayoutMediator.ITabConfigurationStrategy.OnConfigureTab(TabLayout.Tab tab, int position)
		{
			tab.SetText(new String(SectionController.GetItems()[position].Title));
		}

		void UpdateCurrentItem(ShellContent content)
		{
			if (_toolbarTracker == null)
				return;

			var page = ((IShellContentController)content).GetOrCreateContent();
			if (page == null)
				throw new ArgumentNullException(nameof(page), "Shell Content Page is Null");

			ShellSection.SetValueFromRenderer(ShellSection.CurrentItemProperty, content);
			_toolbarTracker.Page = page;
		}

		#endregion IOnPageChangeListener

		#region IAppearanceObserver

		void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
		{
			if (appearance == null)
				ResetAppearance();
			else
				SetAppearance(appearance);
		}

		#endregion IAppearanceObserver

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Verify the ShellContent has a valid ContentTemplate with a DataTemplate that reliably constructs a non-null Page.
  2. If the page type has constructor dependencies (DI), ensure they are registered in MauiProgram and the page is resolvable.
  3. Add error handling/logging inside the DataTemplate factory to catch construction failures.
  4. Test the page type instantiation independently to confirm it constructs without throwing.
  5. Set ShellContent.Content directly to a page instance if ContentTemplate is problematic.

Example fix

// before
new ShellContent
{
    Route = "home",
    ContentTemplate = new DataTemplate(() => null) // factory returns null
}

// after
new ShellContent
{
    Route = "home",
    ContentTemplate = new DataTemplate(() => new HomePage(service))
}
// or set Content directly:
new ShellContent { Content = new HomePage() }
Defensive patterns

Strategy: validation

Validate before calling

// Verify content can be created before assigning
var testContent = ((IShellContentController)shellContent)?.GetOrCreateContent();
if (testContent == null)
    throw new InvalidOperationException("ShellContent failed to create a page.");

Type guard

public static bool ShellContentResolves(ShellContent content)
{
    try { return ((IShellContentController)content)?.GetOrCreateContent() != null; }
    catch { return false; }
}

Prevention

When it happens

Trigger: At line 42: `var page = ((IShellContentController)content).GetOrCreateContent(); if (page == null) throw new ArgumentNullException`. Triggered when: (1) ShellContent.ContentTemplate is null or its DataTemplate factory returns null; (2) the ShellContent's Content property was explicitly set to null; (3) a DataTemplate referencing a type that failed to construct threw silently and returned null; (4) the ShellContent was created programmatically without setting Content or ContentTemplate.

Common situations: 1) ShellContent defined with `ContentTemplate="{x:DataTemplate ...}"` where the DataTemplate's root type threw during construction. 2) ShellContent created in code without Content or ContentTemplate. 3) DI/constructor injection failure in the page type referenced by the DataTemplate (constructor throws, factory returns null). 4) XAML DataTemplate referencing a type that was removed or renamed.

Related errors


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