{"record":{"id":"b297717de8b0667c","repo":"airbnb/epoxy","slug":"model-is-not-added-modeltoinsertbefore","errorCode":null,"errorMessage":"Model is not added: {modelToInsertBefore}","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":155,"sourceCode":"   */\n  protected void addModels(Collection<? extends EpoxyModel<?>> modelsToAdd) {\n    int initialSize = models.size();\n\n    pauseModelListNotifications();\n    models.addAll(modelsToAdd);\n    resumeModelListNotifications();\n\n    notifyItemRangeInserted(initialSize, modelsToAdd.size());\n  }\n\n  /**\n   * Inserts the given model before the other in the {@link #models} list, and notifies that the\n   * item was inserted.\n   */\n  protected void insertModelBefore(EpoxyModel<?> modelToInsert, EpoxyModel<?> modelToInsertBefore) {\n    int targetIndex = getModelPosition(modelToInsertBefore);\n    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    }","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/airbnb/epoxy/blob/e45bd3a61fe3a1f130e184f5b8dcf172ab99025a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java#L137-L173","documentation":"insertModelBefore(modelToInsert, modelToInsertBefore) looks up the position of modelToInsertBefore in the models list via getModelPosition(); if it is not in the list (-1), the insert has no valid anchor, so an IllegalStateException is thrown. The library requires the reference model to already be added.","triggerScenarios":"Calling insertModelBefore with a second argument that was never added to the adapter, or that was removed/filtered out before the call.","commonSituations":"Inserting relative to a model that is added conditionally (e.g. only when data is non-empty); using a new model instance instead of the one registered with the adapter; models cleared on refresh before the insert runs.","solutions":["Ensure the anchor model is added via addModel() before calling insertModelBefore","Check getModelPosition(modelToInsertBefore) != -1 before inserting","Guard against conditional-anchor cases: skip the insert when the anchor model is absent"],"exampleFix":"// before\nadapter.insertModelBefore(newModel, maybeAnchor);\n// after\nif (adapter.getModelPosition(maybeAnchor) != -1) {\n  adapter.insertModelBefore(newModel, maybeAnchor);\n} else {\n  adapter.addModel(newModel);\n}","handlingStrategy":"validation","validationCode":"if (adapter.getModelPosition(anchor) == -1) { throw/skip } else { adapter.insertModelBefore(newModel, anchor); }","typeGuard":null,"tryCatchPattern":"try { adapter.insertModelBefore(newModel, anchor); } catch (IllegalStateException e) { adapter.addModel(newModel); }","preventionTips":["Track which models are currently added; only use present models as insert anchors","Avoid stale model references across clearModels()/refresh cycles"],"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"}