{"record":{"id":"21e36d319d7b1959","repo":"LykosAI/StabilityMatrix","slug":"service-of-type-servicetype-was-registered-as-null","errorCode":null,"errorMessage":"Service of type {serviceType} was registered as null","messagePattern":"Service of type (.+?) was registered as null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/ServiceManager.cs","lineNumber":182,"sourceCode":"        return scopedProviders.TryGetValue(serviceType, out provider);\n    }\n\n    /// <summary>\n    /// Get a view model instance from runtime type\n    /// </summary>\n    [SuppressMessage(\"ReSharper\", \"InconsistentlySynchronizedField\")]\n    public T Get(Type serviceType)\n    {\n        if (!serviceType.IsAssignableTo(typeof(T)))\n        {\n            throw new ArgumentException($\"Service type {serviceType} is not assignable to {typeof(T)}\");\n        }\n\n        if (instances.TryGetValue(serviceType, out var instance))\n        {\n            if (instance is null)\n            {\n                throw new ArgumentException($\"Service of type {serviceType} was registered as null\");\n            }\n            return (T)instance;\n        }\n\n        if (providers.TryGetValue(serviceType, out var provider))\n        {\n            if (provider is null)\n            {\n                throw new ArgumentException($\"Service of type {serviceType} was registered as null\");\n            }\n            var result = provider();\n            if (result is null)\n            {\n                throw new ArgumentException($\"Service provider for type {serviceType} returned null\");\n            }\n            return (T)result;\n        }\n","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ServiceManager.cs#L164-L200","documentation":"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).","triggerScenarios":"Calling Get(serviceType) where instances[serviceType] == null — only reachable if a null was stored via internal registration paths or reflection-based bulk registration.","commonSituations":"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.","solutions":["Fix the registration so a real instance is stored (ensure the singleton is constructed before registration).","Register a provider/factory instead of a null instance so the instance is created lazily on first Get.","Audit any code path that inserts into the instances dictionary for null values."],"exampleFix":"// before\nmanager.instances[typeof(IFoo)] = null; // later Get throws\n// after\nmanager.RegisterSingleton<IFoo, Foo>();","handlingStrategy":"validation","validationCode":"if (manager is ServiceManager<T> sm && sm.HasService<TService>() && !sm.IsInstanceMaterialized<TService>()) throw new InvalidOperationException(\"null instance registered\");","typeGuard":null,"tryCatchPattern":"try { var svc = manager.Get<TService>(); }\ncatch (ArgumentException ex) { logger.Error(ex, \"Null instance registered for {Service}\"); }","preventionTips":["Never store null into the container; register factories for lazily-created services.","Construct singletons before registration.","Audit any reflection/test code that mutates the instances dictionary."],"tags":["dependency-injection","null-reference","invariant"],"backgroundTag":"null-argument","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}