Pages

Pages

Pages

Friday, June 22, 2012

How to resolve UnsupportedOperationException:


 UnsupportedOperationException:

This is One of the most commonly experienced exception by the java programmers who deals with the immutable or unmodifiable objects.In this post lets see how to deal with this exception.Before that let us absorb what exception is this?when it will be thrown? and finally how to fix this exception.

What is meant by UnsupportedOperationException?

The UnsupportedOperationException tells us that a particular operation cannot be performed  as it was restricted for use against a particular object.This exception extends the Runtimeexception which serves as a  super class for a group of run-time exceptions.

When this UnsupportedOperationException is thrown?

As the name implies that this exception is thrown when the requested operation cannot be supported. As i have said before this exception is thrown by almost all of the Concrete collections like.
1.Lists
2.Queue
3.Set
4.Maps.

Let us have a quick overview of java collection frameworks related to our discussion.

All those collections that i have listed above are used to store some values and provides some means to traverse the values and even allows the modification of collection.We can create views for those Collections except queue.

You may wonder what is a view?.A view is a read-only format of the collections,which means that through view we can  traverse the collections and even we can retrieve values.But if you try to modify the collection using view object  this will cause an UnsupportedOperationException to be thrown.For creating Unmodifiable views java provides the following methods.

1.Collections.unmodifiableCollection.
2.Collections.unmodifiableList
3.Collections.unmodifiableSet
4.Collections.unmodifiableSortedSet
5.Collections.unmodifiableMap
6.Collections.unmodifiableSortedMap.

Note:

1.The Map collections allow the use of remove() method but does not support add() method using the view object.
2.The un modifiable views does not support either add() or remove() method.


An another important cause of this exception is the use of wrappers between the collections and the primitive types.

How to overcome UnsupportedOperationException?

As we know the cause of this exception it will be quite easy to deal with it.Since view objects do not allow
modification of the collection,the solution that i suggest you is to use the object of the collection rather than using the view object for modification.

Examples for the UnsupportedOperationException:


Use of Wrappers:

import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
String s[]={"ram","ganesh","paul"};
List li=Arrays.asList(s);
li.clear();
System.out.println(" The values in the list: "+li);
}
}

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractList.remove(Unknown Source)
        at java.util.AbstractList$Itr.remove(Unknown Source)
        at java.util.AbstractList.removeRange(Unknown Source)
        at java.util.AbstractList.clear(Unknown Source)
        at Unsupport.main(Unsupport.java:9)

KeySet View of Map collections:

import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
Map s=new HashMap();
s.put("1","Ram");
s.put("2","Ganesh");
s.put("3","Paul");
System.out.println(s);
Set keys = s.keySet();
keys.add("7");
System.out.println(s);
}
}

{3=Paul, 2=Ganesh, 1=Ram}
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractCollection.add(Unknown Source)
        at Unsupport.main(Unsupport.java:12)

Unmodifiable view of Map collections:

import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
Map s=new HashMap();
s.put("1","Ram");
s.put("2","Ganesh");
s.put("3","Paul");
System.out.println(s);
Map m=Collections.unmodifiableMap(s);
m.put("7","raj");
}
}

{3=Paul, 2=Ganesh, 1=Ram}
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableMap.put(Unknown Source)
        at Unsupport.main(Unsupport.java:12)





16 comments:

  1. helped me solve my problem.. thanks a ton

    ReplyDelete
  2. What was the solution man. I am not able to get solution from this post.

    ReplyDelete
  3. I am getting this error during installation of latest JDK on 64 bit Windows 10 OS. Can someone please help to fix this annoying and un-ubiqutous problem.. cheers

    ReplyDelete
  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Dot Net Coaching Centers in Chennai
    Manual Testing Training in Chennai
    Core Java Training in Chennai
    Best PHP Course in Chennai

    ReplyDelete
  6. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

    ReplyDelete
  7. Your ability to give a balanced viewpoint that takes into account other viewpoints and addresses potential problems stands out above all else. Splunk Certification

    ReplyDelete
  8. You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. DevOps Training

    ReplyDelete
  9. You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. DevOps Certification

    ReplyDelete