airbnb/epoxy · error · IllegalStateException

Model is not added

Error message

Model is not added: {modelToInsertBefore}

What it means

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.

Solutions

  1. Ensure the anchor model is added via addModel() before calling insertModelBefore
  2. Check getModelPosition(modelToInsertBefore) != -1 before inserting
  3. Guard against conditional-anchor cases: skip the insert when the anchor model is absent

Example fix

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

Strategy: validation

Validate before calling

if (adapter.getModelPosition(anchor) == -1) { throw/skip } else { adapter.insertModelBefore(newModel, anchor); }

Try / catch

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

Prevention

When it happens

Trigger: Calling insertModelBefore with a second argument that was never added to the adapter, or that was removed/filtered out before the call.

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

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/b297717de8b0667c. Report an issue: GitHub.

Appendix: source

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

   */
  protected void addModels(Collection<? extends EpoxyModel<?>> modelsToAdd) {
    int initialSize = models.size();

    pauseModelListNotifications();
    models.addAll(modelsToAdd);
    resumeModelListNotifications();

    notifyItemRangeInserted(initialSize, modelsToAdd.size());
  }

  /**
   * Inserts the given model before the other in the {@link #models} list, and notifies that the
   * item was inserted.
   */
  protected void insertModelBefore(EpoxyModel<?> modelToInsert, EpoxyModel<?> modelToInsertBefore) {
    int targetIndex = getModelPosition(modelToInsertBefore);
    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);
    }

View on GitHub (pinned to e45bd3a61f)