IllegalStateException Java Origin: Application & API Last updated 12 Oct 2026

java.lang.IllegalStateException

A method was called when the object wasn’t in the right state for it.

Exact message you may see stream has already been operated upon or closedDuplicate keyCannot call sendError() after the response has been committed
Seen on: Java

Meaning

Examples: iterator.remove() twice, writing to a committed HTTP response, Optional.get misuse in some libraries, Spring context not refreshed. The message explains the state problem.

Common causes

  • Calling methods out of order
  • Response already committed (servlets)
  • Stream already operated upon or closed
  • Duplicate keys in Collectors.toMap

⚡ Quick fix

  1. Read the message for the expected state
  2. Don’t reuse streams
  3. Pass a merge function to Collectors.toMap

Detailed fix by platform

Java

  1. Collectors.toMap(User::getEmail, u -> u, (a, b) -> a)

How to diagnose

  1. Message — Which state is wrong?
  2. Order — Which call came before?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

409 — HTTP 409 Conflict (HTTP)409Actions must be plain objects — Error: Actions must be plain objects. Instead, the actual type was: 'function'. (React)Actions must be plain…ArithmeticException — java.lang.ArithmeticException: / by zero (Java)ArithmeticExceptionError acquiring the state lock — Terraform: Error: Error acquiring the state lock — ConditionalCheckFailedException / state blob is already locked (Terraform)Error acquiring the s…IllegalArgumentException — java.lang.IllegalArgumentException (Java)IllegalArgumentExcept…Index was out of range — ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection (.NET)Index was out of rangenon-serializable value — A non-serializable value was detected in the state / in an action, in the path (React)non-serializable valueNoSuchElementException — java.util.NoSuchElementException: No value present (Java)NoSuchElementExceptionIllegalSt…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.