PrismLibrary/Prism · error · ArgumentNullException

modules

Error message

modules

What it means

The ModuleCatalogBase(IEnumerable<IModuleInfo>) constructor throws ArgumentNullException when the modules collection is null. A catalog must be initialized with at least an empty sequence; null cannot be enumerated into Items.

Solutions

  1. Pass a non-null collection; use Array.Empty<IModuleInfo>() or new List<IModuleInfo>() when there are no modules.
  2. Null-check or coalesce the source collection before constructing the catalog.
  3. Fix the configuration/DI registration so the module list provider returns an empty collection instead of null.

Example fix

// before
var catalog = new ModuleCatalog(GetModulesFromConfig()); // GetModulesFromConfig() may return null
// after
var modules = GetModulesFromConfig() ?? Enumerable.Empty<IModuleInfo>();
var catalog = new ModuleCatalog(modules);
Defensive patterns

Strategy: validation

Validate before calling

IEnumerable<IModuleInfo> modules = sourceModules ?? Enumerable.Empty<IModuleInfo>();
var catalog = new ModuleCatalog(modules);

Type guard

bool HasModules(IEnumerable<IModuleInfo> m) => m != null;

Try / catch

try
{
    var catalog = new ModuleCatalog(modules);
}
catch (ArgumentNullException ex) when (ex.ParamName == nameof(modules))
{
    logger.LogWarning("Null module list; creating empty catalog");
    catalog = new ModuleCatalog();
}

Prevention

When it happens

Trigger: new ModuleCatalog((IEnumerable<IModuleInfo>)null) or passing a null result from a config loader / container resolution into the catalog constructor.

Common situations: Module lists read from XAML, app.config, or a DI container that return null when no modules are configured; calling code that forgets to initialize a module list field.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/32d9e1579be25dd1. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Modularity/ModuleCatalogBase.cs:50

        /// <summary>
        /// Initializes a new instance of the <see cref="IModuleCatalog"/> class.
        /// </summary>
        public ModuleCatalogBase()
        {
            _items = new ModuleCatalogItemCollection();
            _items.CollectionChanged += ItemsCollectionChanged;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IModuleCatalog"/> class while providing an
        /// initial list of <see cref="IModuleInfo"/>s.
        /// </summary>
        /// <param name="modules">The initial list of modules.</param>
        public ModuleCatalogBase(IEnumerable<IModuleInfo> modules)
            : this()
        {
            if (modules == null) throw new ArgumentNullException(nameof(modules));
            foreach (IModuleInfo moduleInfo in modules)
            {
                Items.Add(moduleInfo);
            }
        }

        /// <summary>
        /// Gets the items in the <see cref="IModuleCatalog"/>. This property is mainly used to add <see cref="IModuleInfoGroup"/>s or
        /// <see cref="IModuleInfo"/>s through XAML.
        /// </summary>
        /// <value>The items in the catalog.</value>
        public Collection<IModuleCatalogItem> Items => _items;

        /// <summary>
        /// Gets all the <see cref="IModuleInfo"/> classes that are in the <see cref="IModuleCatalog"/>, regardless
        /// if they are within a <see cref="IModuleInfoGroup"/> or not.
        /// </summary>
        /// <value>The modules.</value>

View on GitHub (pinned to 358118cd64)