dotnet/maui · error · InvalidOperationException

The property cannot be null at this time.

Error message

The property {0} cannot be null at this time.

What it means

Verify.PropertyIsNotNull<T> (where T : class) throws InvalidOperationException (not ArgumentException) when the property value is null. Unlike the argument guards, this is a state guard — it signals that the object's current state is invalid because a required property has not been set. The message is formatted with the property name.

Solutions

  1. Set the required property before invoking the operation that depends on it.
  2. Verify initialization order — ensure all required properties are assigned in the constructor or init path.
  3. Add a state check in your own code (if (obj.Property == null) throw) earlier with a clearer message.

Example fix

// before
chromeWindow.ApplyChrome();

// after
if (chromeWindow.ChromeInfo == null)
{
    throw new InvalidOperationException("ChromeInfo must be set before applying.");
}
chromeWindow.ApplyChrome();
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check: required property must be set before the operation
if (obj.RequiredProperty == null)
{
    throw new InvalidOperationException("RequiredProperty must be set before this operation.");
}
obj.DoOperation();

Try / catch

try
{
    obj.DoOperation();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("cannot be null"))
{
    // log and guide the user to set required properties
    throw;
}

Prevention

When it happens

Trigger: Calling a method that internally asserts Verify.PropertyIsNotNull(_someProperty, "SomeProperty") before the property has been assigned. The guard fires because a precondition on internal state was violated.

Common situations: Using an object before its required properties are set (e.g. calling Show before Title is assigned); a dependency property or service was never injected; initialization ran partially and a setter was skipped.

Related errors


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

Appendix: source

Thrown at src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/Verify.cs:150

		[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		[DebuggerStepThrough]
		public static void IsNull<T>(T obj, string name) where T : class
		{
			if (null != obj)
			{
				Assert.Fail();
				throw new ArgumentException("The parameter must be null.", name);
			}
		}

		[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		[DebuggerStepThrough]
		public static void PropertyIsNotNull<T>(T obj, string name) where T : class
		{
			if (null == obj)
			{
				Assert.Fail();
				throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The property {0} cannot be null at this time.", name));
			}
		}

		[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		[DebuggerStepThrough]
		public static void PropertyIsNull<T>(T obj, string name) where T : class
		{
			if (null != obj)
			{
				Assert.Fail();
				throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The property {0} must be null at this time.", name));
			}
		}

		/// <summary>
		/// Verifies the specified statement is true.  Throws an ArgumentException if it's not.
		/// </summary>
		/// <param name="statement">The statement to be verified as true.</param>

View on GitHub (pinned to f377ff1c5e)