LykosAI/StabilityMatrix · error · ArgumentException

Service of type was registered as null

Error message

Service of type {serviceType} was registered as null

What it means

ServiceManager.Get found the requested type in the instances (singleton) dictionary but the stored value is null. A null instance is an invalid container state, so Get throws ArgumentException rather than returning null (which the non-nullable T contract forbids).

Solutions

  1. Fix the registration so a real instance is stored (ensure the singleton is constructed before registration).
  2. Register a provider/factory instead of a null instance so the instance is created lazily on first Get.
  3. Audit any code path that inserts into the instances dictionary for null values.

Example fix

// before
manager.instances[typeof(IFoo)] = null; // later Get throws
// after
manager.RegisterSingleton<IFoo, Foo>();
Defensive patterns

Strategy: validation

Validate before calling

if (manager is ServiceManager<T> sm && sm.HasService<TService>() && !sm.IsInstanceMaterialized<TService>()) throw new InvalidOperationException("null instance registered");

Try / catch

try { var svc = manager.Get<TService>(); }
catch (ArgumentException ex) { logger.Error(ex, "Null instance registered for {Service}"); }

Prevention

When it happens

Trigger: Calling Get(serviceType) where instances[serviceType] == null — only reachable if a null was stored via internal registration paths or reflection-based bulk registration.

Common situations: Registration code that adds null instances (e.g. a factory variable that was null at registration time); manual dictionary mutation in tests; deserialization or initialization ordering bugs.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/21e36d319d7b1959. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Services/ServiceManager.cs:182

        return scopedProviders.TryGetValue(serviceType, out provider);
    }

    /// <summary>
    /// Get a view model instance from runtime type
    /// </summary>
    [SuppressMessage("ReSharper", "InconsistentlySynchronizedField")]
    public T Get(Type serviceType)
    {
        if (!serviceType.IsAssignableTo(typeof(T)))
        {
            throw new ArgumentException($"Service type {serviceType} is not assignable to {typeof(T)}");
        }

        if (instances.TryGetValue(serviceType, out var instance))
        {
            if (instance is null)
            {
                throw new ArgumentException($"Service of type {serviceType} was registered as null");
            }
            return (T)instance;
        }

        if (providers.TryGetValue(serviceType, out var provider))
        {
            if (provider is null)
            {
                throw new ArgumentException($"Service of type {serviceType} was registered as null");
            }
            var result = provider();
            if (result is null)
            {
                throw new ArgumentException($"Service provider for type {serviceType} returned null");
            }
            return (T)result;
        }

View on GitHub (pinned to af93d6ef57)