Blog |

How to Fix The IllegalStateException in Java

How to Fix The Illegal<wbr>State<wbr>Exception 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 an illegal or inappropriate time.

To use an analogy, invoking a method in Java when the system is not in the appropriate state is like trying to start a car while it is in "Drive". Just as the car will not start because it is unsafe, a method call results in an IllegalStateException when the internal conditions necessary for its execution are not met.

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 methods where this exception can occur when they are called at the wrong time:

  • Thread.start(): When the Thread.start() method is called on a thread that has already been started.
  • Iterator.remove(): 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.
  • Queue.add(): 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 Examples

Example One

Here’s an example of an IllegalStateException 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(); //Trying to remove element from list before calling it.next()
    }
}

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)

Example Two

Here’s an example of an IllegalStateException thrown trying to add an element to a Queue that is already full:

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class IllegalStateExceptionExample {
public static void main(String[] args) {
        Queue<Integer> queue = new ArrayBlockingQueue<>(2); //Create a queue with a fixed capacity of 2

        //Add two elements to fill the queue
        queue.add(1);
        queue.add(2);

        queue.add(3); //Attempt to add another element to the full queue

        System.out.println(queue);
}
}

Here, a Queue is created with a size of 2. Since 3 elements are attempted to be added to the Queue, which is beyond its size, running the above code throws an IllegalStateException:

Exception in thread "main" java.lang.IllegalStateException: Queue full
at java.base/java.util.AbstractQueue.add(AbstractQueue.java:98)
at java.base/java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:329)
at IllegalStateExceptionExample.main(IllegalStateExceptionExample.java:14)

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.

The earlier examples can be updated to ensure this:

Example One

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(); //Calling it.next() to move iterator position before removing element from list
        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]

Example Two

The Queue.offer() function can be used instead of Queue.add() to ensure that the capacity restrictions of the Queue are not violated:

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class IllegalStateExceptionExample {
public static void main(String[] args) {
        Queue<Integer> queue = new ArrayBlockingQueue<>(2);

        queue.add(1);
        queue.add(2);

        queue.offer(3); //Add another element to the queue if possible

        System.out.println(queue);
}
}

The Queue.offer() method ensures that an element is only added to the Queue if it is possible to do so without exceeding capacity limits. Running the above code avoids the exception produces the correct output as expected:

[1, 2]

Track, Analyze and Manage Errors With Rollbar

Managing 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. Try it 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