{"record":{"id":"99881eee691aa7a9","repo":"theonedev/onedev","slug":"this-method-can-only-be-called-on-a-component-that","errorCode":null,"errorMessage":"This method can only be called on a component that has already been added to its parent.","messagePattern":"This method can only be called on a component that has already been added to its parent\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/org/apache/wicket/Component.java","lineNumber":2829,"sourceCode":"\t * @since 1.2.1\n\t * \n\t * @param replacement\n\t *            component to replace this one\n\t * @return the component which replaced this one\n\t */\n\tpublic Component replaceWith(Component replacement)\n\t{\n\t\tArgs.notNull(replacement, \"replacement\");\n\n\t\tif (!getId().equals(replacement.getId()))\n\t\t{\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\"Replacement component must have the same id as the component it will replace. Replacement id [[\" +\n\t\t\t\t\treplacement.getId() + \"]], replaced id [[\" + getId() + \"]].\");\n\t\t}\n\t\tif (parent == null)\n\t\t{\n\t\t\tthrow new IllegalStateException(\n\t\t\t\t\"This method can only be called on a component that has already been added to its parent.\");\n\t\t}\n\t\tparent.replace(replacement);\n\t\treturn replacement;\n\t}\n\n\t/**\n\t * @param component\n\t *            The component to compare with\n\t * @return True if the given component's model is the same as this component's model.\n\t */\n\tpublic final boolean sameInnermostModel(final Component component)\n\t{\n\t\treturn sameInnermostModel(component.getDefaultModel());\n\t}\n\n\t/**\n\t * @param model","sourceCodeStart":2811,"sourceCodeEnd":2847,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/org/apache/wicket/Component.java#L2811-L2847","documentation":"Component.replaceWith() requires the component to already have a parent, because the replacement is delegated to parent.replace(replacement). Calling it on an unattached component (parent == null) throws IllegalStateException — there is no parent in which to perform the replacement.","triggerScenarios":"Calling replaceWith() on a newly created component that was never added to a container; calling it after the component was removed; calling it on detached/serialized components whose parent link was dropped.","commonSituations":"Building a fresh component and calling replaceWith before adding; component removed earlier in the same request then replaced; calling replaceWith in onDetach after hierarchy teardown.","solutions":["Add the original component to its container before ever calling replaceWith on it.","If unattached, just add the new component to the container instead of replaceWith.","Guard with `if (old.getParent() == null) { container.add(replacement); } else { old.replaceWith(replacement); }`.","Ensure replaceWith is invoked during the request/render phase, not teardown.","Verify the component wasn't removed by earlier code in the handler."],"exampleFix":"// before\nLabel fresh = new Label(\"msg\", \"hi\");\nfresh.replaceWith(new Label(\"msg\", \"bye\")); // no parent yet\n// after\ncontainer.add(new Label(\"msg\", \"hi\"));\nlabel.replaceWith(new Label(\"msg\", \"bye\")); // label is attached","handlingStrategy":"type-guard","validationCode":"if (old.getParent() == null) { container.add(replacement); return; }","typeGuard":"boolean replaceable(Component c) { return c.getParent() != null; }","tryCatchPattern":"try { old.replaceWith(replacement); } catch (IllegalStateException e) { log.warn(\"Not attached; adding directly\", e); }","preventionTips":["Only replaceWith components created and added during the same request","Never call replaceWith during onDetach/detach phases","For fresh components, add to the container directly"],"tags":["wicket","component-tree","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}