dotnet/wpf · error · InvalidOperationException
The property cannot be null at this time.
Error message
The property {name} cannot be null at this time. What it means
Verify.PropertyIsNotNull<T> asserts that an instance property is initialized, throwing InvalidOperationException (not ArgumentException, since this is object-state validation, not argument validation) when the property is null. The message names the offending property via string interpolation.
Solutions
- Initialize the property in the constructor or via proper initialization before the guarded call.
- Delay the operation until initialization is complete (e.g. wait for Loaded event in WPF).
- Use lazy initialization or a factory to guarantee non-null.
- Check the interpolated property name in the message to find which property needs initialization.
Example fix
// before Verify.PropertyIsNotNull(this.Document, nameof(this.Document)); Document.DoWork(); // after if (this.Document == null) this.Document = new Document(); Verify.PropertyIsNotNull(this.Document, nameof(this.Document));
Defensive patterns
Strategy: validation
Validate before calling
if (this.Document == null)
throw new InvalidOperationException("Initialize Document before use");
Verify.PropertyIsNotNull(this.Document, nameof(this.Document)); Type guard
static bool IsInitialized<T>(T prop) where T : class => prop is not null;
Try / catch
try {
Verify.PropertyIsNotNull(this.Document, nameof(this.Document));
} catch (InvalidOperationException ex) {
log.Error(ex.Message);
InitializeDocument();
} Prevention
- Initialize all required properties in constructors or Initialize methods.
- In WPF, defer property access until after Loaded/template application.
- Use lazy<T> for expensive required properties.
When it happens
Trigger: Calling Verify.PropertyIsNotNull(obj, name) where obj is null — e.g. asserting this.SomeService != null before use when the property was never assigned (initializer not run, constructor skipped, or XAML/load path not executed yet).
Common situations: Accessing a WPF component property before template/load initialization completes; using a class before its constructor finished; lazy property that was expected to be set by a prior step.
Related errors
- The property must be null at this time.
- Cannot initialize compressor.
- Image_NeitherArgument (UriSource, StreamSource)
- SR.Collection_NoNull
- SR.Collection_NoNull
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b8534dbf86163f8e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Standard/Verify.cs:138
/// <summary>Verifies that an argument is null.</summary>
/// <typeparam name="T">Type of the object to validate. Must be a class.</typeparam>
/// <param name="obj">The object to validate.</param>
/// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
[DebuggerStepThrough]
public static void IsNull<T>(T obj, string name) where T : class
{
if (null != obj)
{
throw new ArgumentException("The parameter must be null.", name);
}
}
[DebuggerStepThrough]
public static void PropertyIsNotNull<T>(T obj, string name) where T : class
{
if (null == obj)
{
throw new InvalidOperationException($"The property {name} cannot be null at this time.");
}
}
[DebuggerStepThrough]
public static void PropertyIsNull<T>(T obj, string name) where T : class
{
if (null != obj)
{
throw new InvalidOperationException($"The property {name} must be null at this time.");
}
}
/// <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>
/// <param name="name">Name of the parameter to include in the ArgumentException.</param>
[DebuggerStepThrough]View on GitHub (pinned to 81131a70a4)