java.lang.IllegalStateException
A method was called when the object wasn’t in the right state for it.
stream has already been operated upon or closedDuplicate keyCannot call sendError() after the response has been committed 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
- Read the message for the expected state
- Don’t reuse streams
- Pass a merge function to Collectors.toMap
Detailed fix by platform
Java
Collectors.toMap(User::getEmail, u -> u, (a, b) -> a)
How to diagnose
- Message — Which state is wrong?
- 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:
Similar errors
Most viewed in Java
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 12 Oct 2026