Blog |

How to Fix The IllegalStateException in Java

How to Fix The IllegalStateException in Java
Table of Contents

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:

import java.util.ArrayList;
import java.util.Iterator;

public class IllegalStateExceptionExample {
    public static void main(String args[]) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");

        Iterator<String> it = list.iterator();
        it.remove();
    }
}

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:

Exception in thread "main" java.lang.IllegalStateException
    at java.base/java.util.ArrayList$Itr.remove(ArrayList.java:980)
    at IllegalStateExceptionExample.main(IllegalStateExceptionExample.java:12)

 

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:

import java.util.ArrayList;
import java.util.Iterator;

public class IllegalStateExceptionExample {
    public static void main(String args[]) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");

        Iterator<String> it = list.iterator();
        it.next();
        it.remove();

        System.out.println(list);  
    }
}

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:

[b, c]

 

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

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!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape