How to Fix Exception in thread main java.lang.IllegalStateException

People are currently reading this guide.
Getting "Exception in thread main java.lang.IllegalStateException"? Want to learn how to fix IllegalStateException? Wait, we have a complete tutorial below. In just a few minutes you can get rid of this "Exception in thread main java.lang.IllegalStateException" message.

Now we will see what position this IllegalStateException exception holds in Java Exceptions. This IllegalStateException  is completely subjective and can likely occur when there is not appropoate timing for an object invocation.

Exception in thread main java.lang.IllegalStateException

What is IllegalStateException?

IllegalStateException is generally used to indicate that a method is called at an illegal or inappropriate time. However, this does not seem to be a common use.

That has mentioned officially on Oracle docs, lets have a look.

Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). This constructor is useful for exceptions that are little more than wrappers for other throwables (for example, PrivilegedActionException).


Which clearly mentions that it flags out the program that a method has invoked at an incorrect time. And may be a wrong object has been processed at that time.

Where does IllegalStateException lie?


java.lang.IllegalStateException

  • java.lang.Object
    • java.lang.Throwable
      • java.lang.Exception
        • java.lang.RuntimeException
          • IllegalStateException

How to fix IllegalStateException?

Have a look at below code. The iterator method() points on array object for element to be returned. If the remove() method is invoked on that element, the element where the cursor is positioned on array is deleted.

If the remove() function is invoked without being next() method is called, then element will be deleted by JVM because the cursor does not point to the element.

Here remove () call is an illegal operation and conflicts the state of an object. But the next() method after remove() method call are legal operations.


Iterator iterator = array.iterator();
while(iterator.hasNext()) {
    Object obj2 = array.next();
    iterator.remove();  
}

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

With threads, have you ever wondered what will happen if you try to start() the thread again. That means, the thread is already started, and you are invoking start() method again on started thread.

Thread t = new Thread();
t.start();
//
//
t.start();

What you will see after executing this is IllegalStateException, as you can not call start() method twice, it will throw IllegalStateException.

Runtime Excpetion: IllegalThreadStateException

Sample Demonstration to fix IllegalStateException

You can refer to this example and easily adjust your code to avoid this IllegalStateException.


for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
  String str = iterator.next();
  if (count == 1) {
    iterator.remove();
  }
  count++;
}

Any Issues? - Live Connect

You have our undying gratitude for your visit!