dotnet/maui · error · ArgumentNullException

application

Error message

application

What it means

FormsApplicationDelegate.LoadApplication throws ArgumentNullException(nameof(application)) when called with null. LoadApplication is the required entry point that binds the Application model to the macOS host; a null app cannot be set as the current application. The nameof produces the terse message 'application'.

Source

Thrown at src/Compatibility/Core/src/MacOS/FormsApplicationDelegate.cs:29

		Application _application;
		bool _isSuspended;
		static int _storyboardMainMenuCount;

		public Func<MenuItem, NSMenuItem> NativeMenuItemCreator { get; set; }
		public abstract NSWindow MainWindow { get; }

		protected override void Dispose(bool disposing)
		{
			if (disposing && _application != null)
				_application.PropertyChanged -= ApplicationOnPropertyChanged;

			base.Dispose(disposing);
		}

		protected void LoadApplication(Application application)
		{
			if (application == null)
				throw new ArgumentNullException(nameof(application));

			Application.SetCurrentApplication(application);
			_application = application;

			if (NSApplication.SharedApplication.MainMenu != null)
				_storyboardMainMenuCount = (int)NSApplication.SharedApplication.MainMenu.Count;

			application.PropertyChanged += ApplicationOnPropertyChanged;
		}

		public override void DidFinishLaunching(Foundation.NSNotification notification)
		{
			if (MainWindow == null)
				throw new InvalidOperationException("Please provide a main window in your app");

			MainWindow.Display();
			MainWindow.MakeKeyAndOrderFront(NSApplication.SharedApplication);
			if (_application == null)

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure a non-null Application is constructed: LoadApplication(new App()); where App derives from Application.
  2. If using DI, resolve and null-check the Application before calling LoadApplication.
  3. Add a guard: if (app == null) throw new InvalidOperationException("App not built"); before LoadApplication to give a clearer message at the source.

Example fix

// before
LoadApplication(_app); // _app is null

// after
_app = new App();
LoadApplication(_app);
Defensive patterns

Strategy: validation

Validate before calling

var app = BuildApp(); // never null
if (app == null) throw new InvalidOperationException("App construction failed");
LoadApplication(app);

Type guard

static bool IsValidApp(Application a) => a != null;

Prevention

When it happens

Trigger: Calling LoadApplication(null) in the app delegate, e.g. because the Application instance was not constructed (constructor threw, field not assigned, or DI returned null).

Common situations: DI/build error producing a null Application. Conditional construction: LoadApplication(_app) where _app stayed null in a code path. Refactor that moved Application creation into a method returning null on failure.

Related errors


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