{"record":{"id":"d08f113e4be58a08","repo":"airbnb/epoxy","slug":"model-is-not-added-modeltoinsertafter","errorCode":null,"errorMessage":"Model is not added: {modelToInsertAfter}","messagePattern":"Model is not added: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java","lineNumber":172,"sourceCode":"    if (targetIndex == -1) {\n      throw new IllegalStateException(\"Model is not added: \" + modelToInsertBefore);\n    }\n\n    pauseModelListNotifications();\n    models.add(targetIndex, modelToInsert);\n    resumeModelListNotifications();\n\n    notifyItemInserted(targetIndex);\n  }\n\n  /**\n   * Inserts the given model after the other in the {@link #models} list, and notifies that the item\n   * was inserted.\n   */\n  protected void insertModelAfter(EpoxyModel<?> modelToInsert, EpoxyModel<?> modelToInsertAfter) {\n    int modelIndex = getModelPosition(modelToInsertAfter);\n    if (modelIndex == -1) {\n      throw new IllegalStateException(\"Model is not added: \" + modelToInsertAfter);\n    }\n\n    int targetIndex = modelIndex + 1;\n    pauseModelListNotifications();\n    models.add(targetIndex, modelToInsert);\n    resumeModelListNotifications();\n\n    notifyItemInserted(targetIndex);\n  }\n\n  /**\n   * If the given model exists it is removed and an item removal is notified. Otherwise this does\n   * nothing.\n   */\n  protected void removeModel(EpoxyModel<?> model) {\n    int index = getModelPosition(model);\n    if (index != -1) {\n      pauseModelListNotifications();","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/airbnb/epoxy/blob/e45bd3a61fe3a1f130e184f5b8dcf172ab99025a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java#L154-L190","documentation":"insertModelAfter(modelToInsert, modelToInsertAfter) resolves modelToInsertAfter's index via getModelPosition(); when the anchor model is not in the models list (-1), an IllegalStateException is thrown because there is no position to insert after. The reference model must already exist in the adapter.","triggerScenarios":"Calling insertModelAfter with a second-argument model that was never added, was removed, or whose add was skipped by a conditional branch.","commonSituations":"Anchor model only added when a header/feature flag is on; using a fresh model instance not tracked by the adapter; ordering bugs where the anchor insert happens after this call.","solutions":["Add the anchor model with addModel() before calling insertModelAfter","Check getModelPosition(modelToInsertAfter) != -1 before the call","Fix ordering so the anchor is always inserted first, or fall back to addModel() when absent"],"exampleFix":"// before\nadapter.insertModelAfter(newModel, footerModel);\n// after\nif (adapter.getModelPosition(footerModel) != -1) {\n  adapter.insertModelAfter(newModel, footerModel);\n} else {\n  adapter.addModel(newModel);\n}","handlingStrategy":"validation","validationCode":"if (adapter.getModelPosition(anchor) != -1) { adapter.insertModelAfter(newModel, anchor); }","typeGuard":null,"tryCatchPattern":"try { adapter.insertModelAfter(newModel, anchor); } catch (IllegalStateException e) { adapter.addModel(newModel); }","preventionTips":["Add the anchor model before any relative insert","Keep model add/remove and relative inserts in one ordered update block"],"tags":["android","epoxy","model-not-added","illegal-state"],"backgroundTag":"entity-not-found","analyzedSha":"e45bd3a61fe3a1f130e184f5b8dcf172ab99025a","analyzedAt":"2026-09-13T03:24:16.052Z","contentChangedAt":"2026-09-13T03:24:16.052Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}