• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException in Navigable set ceiling method()  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there



this is my code and I am trying to read value that are value less than 6 but on console it is showing

ClassCastException

I read this document

http://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html

but I am not able to find out where is bug.

any help?


Thanks

 
Mike Degteariov
Ranch Hand
Posts: 145
8
Java Mac MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to cast the result of lower to NavigableSet on line 12.

that method should return String, so the cast fails.

http://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html

lower Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i made few changes




but it is still showing same error..

any other suggestions??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
but it is still showing same error..

any other suggestions??


Hint #1: If you print the stacktrace, it will tell you the exact line where it is happening.

Hint #2: Not that it matters, as you only have one location in the whole program that actually cast anything (so that it may be a good place to start).

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok this is my stack trace


Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.kishor.navigableSet.NavigableSetDemo.main(NavigableSetDemo.java:31)



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:Ok this is my stack trace


Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.kishor.navigableSet.NavigableSetDemo.main(NavigableSetDemo.java:31)


Yeah. That's a one line stacktrace, with a pretty clear explanation of the issue. If you are going to learn how to read a stacktrace, it is not going to get easier than that.

Henry
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my problem is not yet solved can any one give a simple example of using ceiling() method in navigableSet

element should be add
using
setA.add();


and then rest of code for ceiling.


 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:ok i made few changes . . .
Those changes look as if you were guessing. You can guess a million times and one of those guesses will probably be correct.

Or you can think about the problem and get it right first time.
You are not geting the same error. It might be the same Exception but it is a different error because the two posts show Sets filled with different types. The first Strings, the second Integers. Rather than guessing you should be getting the return value from the lower call and exploring it.
Yes, that code will work. I have tried it.
Now read the documentation for NavigableSet#lower and you find out it returns an E. Since you haven't specified types for the Set, you are returning an java.lang.Object.
Try changing NavigableSet setA to NavigableSet<String> setA (first code only) and you can see it will return a String. Then how could you cast that to a NavigableSet?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you would probably get a compile‑time error if you had used generics.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks campbell

it works
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!