dotnet/wpf · error · ArgumentNullException

ArgumentNullException

Error message

ArgumentNullException

What it means

The ResourcePart constructor validates that the ResourceManagerWrapper it is given is non-null before storing it. Passing null means the part would have no way to locate its resource stream, so the constructor fails fast with ArgumentNullException naming the rmWrapper parameter.

Solutions

  1. Ensure a valid ResourceManagerWrapper is constructed and passed to ResourcePart
  2. Check that the ResourceManager/assembly used to build the wrapper initialized correctly
  3. If constructing via reflection, supply the fourth argument explicitly

Example fix

// before
var part = new ResourcePart(container, uri, name, null);
// after
var wrapper = new ResourceManagerWrapper(resourceManager);
var part = new ResourcePart(container, uri, name, wrapper);
Defensive patterns

Strategy: validation

Validate before calling

if (rmWrapper == null) throw new InvalidOperationException("ResourceManagerWrapper must be initialized before creating a ResourcePart");

Type guard

bool IsValid(ResourceManagerWrapper w) => w is not null;

Try / catch

try { var part = new ResourcePart(c, uri, name, rmWrapper); } catch (ArgumentNullException) { /* init wrapper first */ }

Prevention

When it happens

Trigger: Calling new ResourcePart(container, uri, name, null), or an internal ResourceContainer.CreatePartCore path where a null ResourceManagerWrapper is supplied.

Common situations: Custom AppModel/package plumbing built on internal WPF APIs; initialization ordering bugs where the ResourceManager factory returns null; reflection-based construction with missing arguments.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/dfa305552da70d91. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ResourcePart.cs:30

    /// <summary>
    /// ResourcePart is an implementation of the abstract PackagePart class. It contains an override for GetStreamCore.
    /// </summary>
    internal class ResourcePart : System.IO.Packaging.PackagePart
    {
        //------------------------------------------------------
        //
        //  Public Constructors
        //
        //------------------------------------------------------

        #region Public Constructors

        public ResourcePart(Package container, Uri uri, string name, ResourceManagerWrapper rmWrapper) :
            base(container, uri)
        {
            if (rmWrapper == null)
            {
                throw new ArgumentNullException(nameof(rmWrapper));
            }

            _rmWrapper = rmWrapper;
            _name = name;
        }

        #endregion

        //------------------------------------------------------
        //
        //  Protected Methods
        //
        //------------------------------------------------------

        #region Protected Methods

        protected override Stream GetStreamCore(FileMode mode, FileAccess access)
        {

View on GitHub (pinned to 81131a70a4)