How to Fix The IllegalStateException in Java
An IllegalStateException is a runtime exception in Java that is thrown to indicate that a method has been invoked at the wrong time. This exception is used to signal that a method is called at an illegal or inappropriate time.
For example, once a thread has been started, it is not allowed to restart the same thread again. If such an operation is performed, the IllegalStateException is thrown.
Since the IllegalStateException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor.
What Causes IllegalStateException
The IllegalStateException is thrown when the Java environment or application is not in an appropriate state for the requested operation. This can occur when dealing with threads or the Collections framework of the java.util package under specific conditions. Here are examples of some situations where this exception can occur:
- When the Thread.start() method is called on a thread that has already been started.
- When the remove() method of the Iterator interface is called on a List without calling the next() method. This leaves the List collection in an unstable state, causing an IllegalStateException .
- If an element is attempted to be added to a Queue that is full. Adding elements beyond the size of the queue will cause an IllegalStateException .
IllegalStateException Example
Here’s an example of an IllegalMonitorStateException thrown when the Iterator.remove() method is called to remove an element from an ArrayList before calling the next() method:
Since the remove() method is used to remove the previous element being referred to by the Iterator , the next() method should be called before an element is attempted to be removed. In this case, the next() method was never called, so the Iterator attempts to remove the element before the first element.
Since this action is illegal, running the above code throws an IllegalStateException :
How to Fix IllegalStateException
To avoid the IllegalStateException in Java, it should be ensured that any method in code is not called at an illegal or inappropriate time.
In the above example, calling the Iterator.next() method on the ArrayList before using the remove() method to remove an element from it will help fix the issue:
Calling the next() method moves the Iterator position to the next element. Calling the remove() method afterwards will remove the first element in the ArrayList , which is a legal operation and helps fix the exception.
Running the above code produces the correct output as expected:
Track, Analyze and Manage Errors With Rollbar

Managing Java errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!
How to fix an android 'IllegalStateException' error?
I am very new to android and just starting to understand how to develop simple apps ( AndroidStudio , Ubuntu 14.04 , LG G3). From a main activity I want to start another activity (i.e. to show a different screen where the user can make some input) following this solution. In the file MainActivity.java I have the following method:
And the file NewEntryActivity.java is defined as follows:
No error is indicated for this file (everything seems to be defined properly). When I start the app on my phone, the main activity starts without problem, but when I choose the menu item to start the other activity the app closes immediately and I see an error:
Class IllegalStateException
Note that the detail message associated with cause is not automatically incorporated in this exception’s detail message.
IllegalStateException
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2022, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Java lang illegalstateexception как исправить
So in order to fix and avoid Exception in thread main java.lang.IllegalStateException, below is the answer.
iterator.next();
iterator.remove();
We need next() method call before we try to delete element with remove() method. So this clears the cloud and fixes this issue.
IllegalStateException with Threads
Sample Demonstration to fix IllegalStateException
You can refer to this example and easily adjust your code to avoid this IllegalStateException.