Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[Solved] java.lang.IllegalStateException in Java with Examples

In this post, I will be sharing how to solve java.lang.IllegalStateException in Java with examples. According to Oracle docs, IllegalStateException signals that a method has been invoked at an illegal or inappropriate time. In simple words, the Java application or Java environment is not in an appropriate state for the requested operation. IllegalStateException is an unchecked exception and was introduced in JDK (Java development kit) 1.1 version.

Read Also: [Fixed] java.lang.IllegalArgumentException in Java

[Fixed] java.lang.IllegalStateException in Java with Examples

As always, first, we will produce the java.lang.IllegalStateException before moving on to the solution.

1. Producing the exception by calling the remove() method of the iterator interface


According to Oracle docs, the remove() method of the Iterator interface throws java.lang.IllegalStateException if the next() method has not yet been called, or the remove() method has already been called after the last call to the next method.

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

public class IllegalStateExceptionExample
{
public static void main(String args[])
{
ArrayListString> arrList = new ArrayList();
arrList.add("Alive");
arrList.add("is");
arrList.add("Awesome");

IteratorString> it = arrList.iterator();
it.remove();
      System.out.println(arrList);
}}


Output:

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

Explanation


In the above example, we are calling the iterator.remove() method to remove the element from the ArrayList before calling the next() method. As we already know the remove() method is used to remove the previous element being referred to by the iterator, the next() method must be called before an element is attempted to be removed. In the above example, we never called the next() method, so, the iterator attempts to remove the element before the first element. As a result, IllegalStateException was produced.

Solution


We can easily get rid of the java.lang.IllegalStateException by calling the Iterator.next() method on the ArrayList before using the remove() method to remove an element as shown below in the example.

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

public class IllegalStateExceptionExample
{
public static void main(String args[])
{
ArrayListString> arrList = new ArrayList();
arrList.add("Alive");
arrList.add("is");
arrList.add("Awesome");

IteratorString> it = arrList.iterator();
it.next();
it.remove();
System.out.println(arrList);
}
}


Output:

[is, Awesome]

2. Producing the exception by attempting to add an Element to the Queue that is already full


According to Oracle docs, add() method inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

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

public class IllegalStateExceptionExample2
{
public static void main(String args[])
{
int capacity = 3;
// Creating ArrayBlockingQueue object
QueueString> q = new ArrayBlockingQueue(capacity);
// Adding 3 elements to the Queue
q.add("Alive");
q.add("is");
q.add("Awesome");

// Adding an element beyond the capacity of the Queue
q.add(" Be in present");
System.out.println(q);
}
}


Output:

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

Explanation


In the above example, ArrayBlockingQueue's add(E) method inserts the specified element at the tail of the queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and throwing an IllegalStateException if the queue is full.

Solution


We can easily get rid of the java.lang.IllegalStateException by using the offer(E) method instead of add(E) as shown below in the example. offer(E) method inserts the specified element at the tail of the queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if the queue is full. According to Oracle docs, offer(E) method is generally preferable to method add(E), which can fail to insert an element only by throwing an exception.

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

public class IllegalStateExceptionExample2
{
public static void main(String args[])
{
int capacity = 3;
// Creating ArrayBlockingQueue object
QueueString> q = new ArrayBlockingQueue(capacity);
// Adding 3 elements to the Queue
q.add("Alive");
q.add("is");
q.add("Awesome");

// Adding an element beyond the capacity of the Queue
q.offer(" Be in present");
System.out.println(q);
}
}


Output:

[Alive, is, Awesome]

That's all for today. Please mention in the comments if you have any questions related to how to fix java.lang.IllegalStateException in Java with examples.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

[Solved] java.lang.IllegalStateException in Java with Examples

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×