Add to Google Reader or Homepage

Handling java.lang.illegalstateexception

java.lang.illegalstateexception:

In this post i am going to discuss about the java.lang.illegalstateexception. Let us see What is meant by this exception?When will be this exception is thrown? and how to fix it?

What is meant by illegalstateexception?

As you read the name of this exception you can able to understand that a particular framework used in your program is under illegal state.Lets have a look at this exception in detail.

This exception may arise in your program mostly if you are dealing with the collection framework of java.util.package.There are many collections like List,Queue,Tree,Maps.Out of which List and Queues(Queue and Deque) tend to throw this illegal state exception at particular conditions.Note that this exception is not just limited to this Collection Framewok.

When will be this exception is thrown?

In java API it is given that "this exception will be thrown, when we try to invoke a particular method at an inappropriate time."Let us see what are the situations on which this exception will be thrown.

1.In case of List collection we use next() method of the Iterator interface to traverse through the
List.We know about the remove() method which is used to remove a particular item in the list.

In order to remove the first  object from the list that contains string objects,we have to follow these steps:

List li=new LinkedList();
-----
-------
Iterator i=li.iterator();
i.next();   // visits the object
i.remove();//removes the object

If you call the remove() method before (or without) calling the next() method then this illegal state exception will be thrown as it will leave the List collection in an unstable state.


Similarly, if you want to modify a particular object we will use the set() method of the ListIterator() interface as follows:

List li=new LinkedList();
-----
-------
ListIterator i=li.listIterator();
i.next(); //visits the object
i.set("hello");//modifies the object

Note:

"If you have modified the list after visiting the object ,then using the set() method will throw this illegalstateexception" .

Have a look at this,

ListIterator i=li.listIterator();
i.next(); //visits the object
i.remove();//this statement removes the object thereby modifying the list structure.
i.set("hello");//Now,using this set() method will throw an illegalstateexception.

2.In case of queues, if you try to add an element to a queue,then you must ensure that the queue is not full.If this queue is full then we cannot add that element, thus causing this exception to be thrown.

Examples situations of this exception:

Queue:


import java.util.*;
import java.util.concurrent.*;
public class Unsupport
{
public static void main(String args[])
{
BlockingQueue s=new ArrayBlockingQueue(3); //queue size is 3
s.add("Ram");
s.add("Ganesh");
s.add("Paul");
s.add("praveen");
s.add("Banu");
System.out.println(s);
}
}


Exception in thread "main" java.lang.IllegalStateException: Queue full
        at java.util.AbstractQueue.add(Unknown Source)
        at java.util.concurrent.ArrayBlockingQueue.add(Unknown Source)
        at Unsupport.main(Unsupport.java:11)

In the above program i have set the queue size equal to size 3 ,thus adding elements beyond the size will throw this exception.

LinkedList:


import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
List s=new LinkedList();
s.add("Ram");
s.add("Ganesh");
s.add("Paul");
s.add("praveen");
Iterator i=s.iterator();
i.remove(); // the next() method is not used before the remove() method   
System.out.println(s);
}
}

Exception in thread "main" java.lang.IllegalStateException
        at java.util.LinkedList$ListItr.remove(Unknown Source)
        at Unsupport.main(Unsupport.java:12)

Here i forgot to use the next() method which causes this exception.


import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
List s=new LinkedList();
s.add("Ram");
s.add("Ganesh");
s.add("Paul");
s.add("praveen");
ListIterator i=s.listIterator();
i.next();
i.remove();// modifying the list
i.set("kalai");
System.out.println(s);
}
}

Exception in thread "main" java.lang.IllegalStateException
        at java.util.LinkedList$ListItr.set(Unknown Source)
        at Unsupport.main(Unsupport.java:14)

In the above program i have tried to modify the list after using the next() method which leads to this exception.

As i have told before when using set() method we have to ensure that no other actions that modifies the List is used.

Solutions:

As you all understand,when this exception will be thrown? the solution is obvious that we should avoid invoking the methods at inappropriate places.

 

1 comments:

Unknown said...

Well written. This was very helpful to me.

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top