{"record":{"id":"fd46f7e141045b1a","repo":"dotnet/aspnetcore","slug":"the-component-activator-returned-a-null-value-for","errorCode":null,"errorMessage":"The component activator returned a null value for a component of type {componentType.FullName}.","messagePattern":"The component activator returned a null value for a component of type (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/ComponentFactory.cs","lineNumber":81,"sourceCode":"        {\n            // Typical case where no rendermode is specified in either location. We don't call ResolveComponentForRenderMode in this case.\n            component = _componentActivator.CreateInstance(componentType);\n        }\n        else\n        {\n            // At least one rendermode is specified. We require that it's exactly one, and use ResolveComponentForRenderMode with it.\n            var effectiveRenderMode = callerSpecifiedRenderMode is null\n                ? componentTypeRenderMode!\n                : componentTypeRenderMode is null\n                    ? callerSpecifiedRenderMode\n                    : throw new InvalidOperationException($\"The component type '{componentType}' has a fixed rendermode of '{componentTypeRenderMode}', so it is not valid to specify any rendermode when using this component.\");\n            component = _renderer.ResolveComponentForRenderMode(componentType, parentComponentId, _componentActivator, effectiveRenderMode);\n        }\n\n        if (component is null)\n        {\n            // The default activator/resolver will never do this, but an externally-supplied one might\n            throw new InvalidOperationException($\"The component activator returned a null value for a component of type {componentType.FullName}.\");\n        }\n\n        if (!_propertyInjectionDisabled)\n        {\n            PerformPropertyInjection(serviceProvider, component);\n        }\n\n        return component;\n    }\n\n    private void PerformPropertyInjection(IServiceProvider serviceProvider, IComponent instance)\n    {\n        // Suppressed with \"pragma warning disable\" so ILLink Roslyn Anayzer doesn't report the warning.\n#pragma warning disable IL2072 // 'componentType' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'IComponentPropertyActivator.GetActivator(Type)'.\n        var propertyActivator = _propertyActivator.GetActivator(instance.GetType());\n#pragma warning restore IL2072\n\n        propertyActivator(serviceProvider, instance);","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/ComponentFactory.cs#L63-L99","documentation":"Thrown by ComponentFactory.InstantiateComponent when a custom IComponentActivator.CreateInstance returns null. The default activator never returns null, so this only happens with a developer-supplied IComponentActivator registered in DI. The null check is at ComponentFactory.cs:78-82.","triggerScenarios":"Registering a custom IComponentActivator whose CreateInstance returns null for some component type — e.g., a factory that returns null when it doesn't recognize a type instead of falling back.","commonSituations":"Custom DI/activator for view-model-style components that has a missing fallback; conditional activation logic that returns default instead of throwing or delegating.","solutions":["Make your custom IComponentActivator.CreateInstance never return null — fall back to Activator.CreateInstance or throw a descriptive exception.","If using a custom activator as a wrapper, delegate to the default activator for unknown types.","Register the activator correctly in DI so it handles all component types in use."],"exampleFix":"// before\npublic class MyActivator : IComponentActivator\n{\n    public IComponent CreateInstance(Type t)\n        => _cache.TryGetValue(t, out var c) ? c : null; // returns null -> throws\n}\n\n// after\npublic class MyActivator(IComponentActivator defaultActivator) : IComponentActivator\n{\n    public IComponent CreateInstance(Type t)\n        => _cache.TryGetValue(t, out var c) ? c : defaultActivator.CreateInstance(t);\n}","handlingStrategy":"try-catch","validationCode":"var instance = _customActivator.CreateInstance(componentType);\nif (instance is null)\n    throw new InvalidOperationException($\"Activator returned null for {componentType}.\");","typeGuard":null,"tryCatchPattern":"try { component = _componentActivator.CreateInstance(componentType); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"returned a null value\"))\n{\n    logger.LogError(ex, \"Custom component activator returned null for {Type}\", componentType);\n    throw;\n}","preventionTips":["Custom IComponentActivator implementations should always fall back to a default, never return null.","Unit-test the activator against all component types used in the app."],"tags":["blazor","di","component-activator","configuration"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}