aspose file tools*
The moose likes Java in General and the fly likes Why i am getting this exception ...Can any one describe ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why i am getting this exception ...Can any one describe ?" Watch "Why i am getting this exception ...Can any one describe ?" New topic
Author

Why i am getting this exception ...Can any one describe ?

Santanu Guha
Greenhorn

Joined: Jul 28, 2008
Posts: 14
public class Explorer3 {
public static void main(String[] args) {
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = null;
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
System.out.println("S:-"+s);
subs = (TreeSet)s.subSet(608 , 611);

subs.add(new Integer(629)); -----------------Here i am getting the below error !!!
System.out.println(s + " " + subs);
}
}


Exception : ----


S:-[606, 608, 610, 612]
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
at java.util.TreeMap$SubMap.put(TreeMap.java:891)
at java.util.TreeSet.add(TreeSet.java:210)
at dump.Explorer3.main(Explorer3.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4491
    
    8

Have you read the documentation for java.util.TreeSet#subSet(E, E)? It explains exactly why you get that exception.
Santanu Guha
Greenhorn

Joined: Jul 28, 2008
Posts: 14
Matthew Brown wrote:Have you read the documentation for java.util.TreeSet#subSet(E, E)? It explains exactly why you get that exception.


In java doc the below is written :-

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Equivalent to subSet(fromElement, true, toElement, false).

*** This is not clear --> "outside its range"
what should be the range ?
Santanu Guha
Greenhorn

Joined: Jul 28, 2008
Posts: 14
Santanu Guha wrote:
Matthew Brown wrote:Have you read the documentation for java.util.TreeSet#subSet(E, E)? It explains exactly why you get that exception.


In java doc the below is written :-

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Equivalent to subSet(fromElement, true, toElement, false).

*** This is not clear --> "outside its range"
what should be the range ?


clear.resolved .....

Thanks

santanu
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

See the range is the range of the subset which you have set, since 629 is outside the range of subset (608 to 611, where 608 is inclusive and 611 is exclusive) thats why you are getting the exception.
Santanu Guha
Greenhorn

Joined: Jul 28, 2008
Posts: 14
Ankush Kaundal wrote:See the range is the range of the subset which you have set, since 629 is outside the range of subset (608 to 611, where 608 is inclusive and 611 is exclusive) thats why you are getting the exception.


Thank you Ankush Kaundal very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why i am getting this exception ...Can any one describe ?