airbnb/epoxy · error · IllegalStateException

Model is not added

Error message

Model is not added: {modelToInsertAfter}

What it means

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.

Solutions

  1. Add the anchor model with addModel() before calling insertModelAfter
  2. Check getModelPosition(modelToInsertAfter) != -1 before the call
  3. Fix ordering so the anchor is always inserted first, or fall back to addModel() when absent

Example fix

// before
adapter.insertModelAfter(newModel, footerModel);
// after
if (adapter.getModelPosition(footerModel) != -1) {
  adapter.insertModelAfter(newModel, footerModel);
} else {
  adapter.addModel(newModel);
}
Defensive patterns

Strategy: validation

Validate before calling

if (adapter.getModelPosition(anchor) != -1) { adapter.insertModelAfter(newModel, anchor); }

Try / catch

try { adapter.insertModelAfter(newModel, anchor); } catch (IllegalStateException e) { adapter.addModel(newModel); }

Prevention

When it happens

Trigger: Calling insertModelAfter with a second-argument model that was never added, was removed, or whose add was skipped by a conditional branch.

Common situations: 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.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of airbnb/epoxy@e45bd3a61f (2026-09-13). Data as JSON: /api/errors/d08f113e4be58a08. Report an issue: GitHub.

Appendix: source

Thrown at epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java:172

    if (targetIndex == -1) {
      throw new IllegalStateException("Model is not added: " + modelToInsertBefore);
    }

    pauseModelListNotifications();
    models.add(targetIndex, modelToInsert);
    resumeModelListNotifications();

    notifyItemInserted(targetIndex);
  }

  /**
   * Inserts the given model after the other in the {@link #models} list, and notifies that the item
   * was inserted.
   */
  protected void insertModelAfter(EpoxyModel<?> modelToInsert, EpoxyModel<?> modelToInsertAfter) {
    int modelIndex = getModelPosition(modelToInsertAfter);
    if (modelIndex == -1) {
      throw new IllegalStateException("Model is not added: " + modelToInsertAfter);
    }

    int targetIndex = modelIndex + 1;
    pauseModelListNotifications();
    models.add(targetIndex, modelToInsert);
    resumeModelListNotifications();

    notifyItemInserted(targetIndex);
  }

  /**
   * If the given model exists it is removed and an item removal is notified. Otherwise this does
   * nothing.
   */
  protected void removeModel(EpoxyModel<?> model) {
    int index = getModelPosition(model);
    if (index != -1) {
      pauseModelListNotifications();

View on GitHub (pinned to e45bd3a61f)