{"record":{"id":"2e0f3df300158177","repo":"SonarSource/sonarqube","slug":"no-parent-container","errorCode":null,"errorMessage":"No parent container","messagePattern":"No parent container","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java","lineNumber":131,"sourceCode":"      }\n    }\n    return this;\n  }\n\n  @Override\n  public void addWebApiV2ConfigurationClass(Class<?> clazz) {\n    webConfigurationClasses.add(clazz);\n  }\n\n  @Override\n  public Set<Class<?>> getWebApiV2ConfigurationClasses() {\n    return Set.copyOf(webConfigurationClasses);\n  }\n\n  @Override\n  public <T> T getParentComponentByType(Class<T> type) {\n    if (parent == null) {\n      throw new IllegalStateException(\"No parent container\");\n    } else {\n      return parent.getComponentByType(type);\n    }\n  }\n\n  @Override\n  public <T> List<T> getParentComponentsByType(Class<T> type) {\n    if (parent == null) {\n      throw new IllegalStateException(\"No parent container\");\n    } else {\n      return parent.getComponentsByType(type);\n    }\n  }\n\n  private <T> void registerInstance(T instance) {\n    Supplier<T> supplier = () -> instance;\n    Class<T> clazz = (Class<T>) instance.getClass();\n    context.registerBean(componentKeys.ofInstance(instance), clazz, supplier);","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java#L113-L149","documentation":"getParentComponentByType() throws this IllegalStateException when the container has no parent (parent == null). The method is only meaningful in hierarchical (e.g. web/app) container setups where a child container delegates lookups to its parent; a root container cannot fulfill a parent lookup, so it fails fast instead of returning null.","triggerScenarios":"Calling container.getParentComponentByType(SomeType.class) on a root container created without a parent argument (e.g. new SpringComponentContainer(config) with no parent).","commonSituations":"Code shared between web-server child containers and standalone/root contexts calling parent lookups unconditionally; initializing a component at the wrong container level so it runs in a container without a parent.","solutions":["Move the component that performs the lookup into a child container that actually has a parent.","Guard the call: check for parent availability or use getComponentByType on the local container when at root level.","Restructure component wiring so root-level components do not depend on parent-level components."],"exampleFix":"// before\nParentService s = container.getParentComponentByType(ParentService.class);\n// after\nParentService s = container.hasParent()\n  ? container.getParentComponentByType(ParentService.class)\n  : container.getComponentByType(ParentService.class);","handlingStrategy":"type-guard","validationCode":"if (container.isRoot()) {\n  throw new IllegalStateException(\"Cannot do parent lookup at root container\");\n}","typeGuard":"ParentService lookupOrNull(SpringComponentContainer c, Class<ParentService> t) {\n  return c.isRoot() ? null : c.getParentComponentByType(t);\n}","tryCatchPattern":"try {\n  return container.getParentComponentByType(type);\n} catch (IllegalStateException e) {\n  return container.getComponentByType(type); // root-level fallback\n}","preventionTips":["Only perform parent lookups from components registered in child containers.","Design shared startup code to branch on container level.","Document which components require a hierarchical container."],"tags":["spring","dependency-injection","container-hierarchy"],"backgroundTag":"internal-invariant-violation","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}