dotnet/wpf · error · ArgumentNullException

SR.Format(SR.NullBaseUriParam, "baseUri", "location")

Error message

SR.Format(SR.NullBaseUriParam, "baseUri", "location")

What it means

Fonts.GetFontFamilies throws this ArgumentNullException when 'location' is relative (not an absolute Uri) and 'baseUri' is null, because a relative location cannot be resolved without a base. The message names both parameters to explain the relationship.

Solutions

  1. Supply an absolute baseUri: GetFontFamilies(new Uri("pack://application:,,,/MyApp;component/"), "resources/fonts/").
  2. Make 'location' itself an absolute Uri string instead.
  3. For machine fonts, use system font enumeration rather than an empty baseUri.

Example fix

// before
Fonts.GetFontFamilies(null, "resources/fonts/");
// after
Fonts.GetFontFamilies(new Uri("pack://application:,,,/MyApp;component/"), "resources/fonts/");
Defensive patterns

Strategy: validation

Validate before calling

if (baseUri == null && string.IsNullOrEmpty(location))
    throw new ArgumentNullException(nameof(baseUri));
if (baseUri == null && Uri.TryCreate(location, UriKind.Relative, out _))
    throw new ArgumentException("location is relative; baseUri required", nameof(baseUri));

Try / catch

try { Fonts.GetFontFamilies(baseUri, location); }
catch (ArgumentNullException) { /* supply an absolute baseUri for relative locations */ }

Prevention

When it happens

Trigger: Calling GetFontFamilies(null, "resources/fonts/") — location is a relative path and no base Uri was supplied.

Common situations: Assuming the method defaults the base to the application directory or Windows fonts folder when only a relative path is given.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Fonts.cs:102

                throw new ArgumentException(SR.UriNotAbsolute, nameof(baseUri));

            // Determine the font location from the base URI and location string.
            Uri fontLocation;
            if (!string.IsNullOrEmpty(location) && Uri.TryCreate(location, UriKind.Absolute, out fontLocation))
            {
                // absolute location; make sure we support absolute font family references for this scheme
                if (!Util.IsSupportedSchemeForAbsoluteFontFamilyUri(fontLocation))
                    throw new ArgumentException(SR.InvalidAbsoluteUriInFontFamilyName, nameof(location));

                // make sure the absolute location is a valid URI reference rather than a Win32 path as
                // we don't support the latter in a font family reference
                location = fontLocation.GetComponents(UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
            }
            else
            {
                // relative location; we need a base URI
                if (baseUri == null)
                    throw new ArgumentNullException(nameof(baseUri), SR.Format(SR.NullBaseUriParam, "baseUri", "location"));

                // the location part must include a path component, otherwise we'll look in windows fonts and ignore the base URI
                if (string.IsNullOrEmpty(location))
                    location = "./";
                else if (Util.IsReferenceToWindowsFonts(location))
                    location = "./" + location;

                fontLocation = new Uri(baseUri, location);
            }

            // Create the font families.
            return CreateFamilyCollection(
                fontLocation,   // fontLocation
                baseUri,        // fontFamilyBaseUri
                location        // fontFamilyLocationReference
                );
        }

View on GitHub (pinned to 81131a70a4)