I am typecasting the object to a bean class ,which in turn gives me a exception ClassCastException,i am not getting the issue behind this.
Hashtable listEvent = getEvents(label1.getText(), str);
Enumeration events = listEvent.keys();
while (events.hasMoreElements()) {
String key = (String) events.nextElement();
if (key.equals(label1.getText())) {
Vector object = (Vector) listEvent.get(key);
Enumeration hashtable = listEvent.keys();
while (hashtable.hasMoreElements()) {
String keys = (String) hashtable.nextElement();
if (keys.equals(label1.getText())) {
Vector data = (Vector) listEvent.get(keys);
for (int i = 0; i < data.size(); i++) {
EventsBean bean1 = (EventsBean) data.elementAt(i);
I get an error while the running the application for typecasting to EventsBean.
First of all, if you used generic collections rather than raw types (
Map<String, List<EventsBean>>instead ofHashtable), those errors would be caught by the compiler.Forget about Vector, Hashtable and Enumeration. They should not be used anymore since Java 1.2.
And finally, why are you iterating on the hash table keys, instead of looking up the value directy?
Instead of
Just use
Now to answer your question: if you get a ClassCastException, it means that the objects stored in the collection are not what you think they are. Check the code which fills the collection. (and switch to generic, modern collections)