• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class Cast Exception for Map.Entry

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please anyone tell me what am I doing wrong here to get a Class Cast Exception ...

1. Map fieldAttachmentMap = new HashMap();
2. Iterator iter = fieldAttachmentMap.keySet().iterator();
3. while (iter.hasNext()) {
4. Map.Entry entry = (Map.Entry) iter.next();
5. fieldAttachmentInfo.append(entry.getValue() + ":");
6. fieldAttachmentInfo.append(entry.getKey() + "|"); }

I am getting taht error at line 4
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the javadocs of Map.keySet(). What it returns is not a collection of Map.Entry objects. There is another method in that interface that returns what you want.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
entrySet() contains Map.Entry instances.
 
Priya Singh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I am still unable to get my data appripriate with entrySet(), what should I do to have the value/key from KeySet.. instead of Map.Entry, is there any other way I can retrieve that ?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you added anything into your HashMap??
 
Priya Singh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it .. thanks .. thru this ...
Iterator iter = fieldAttachmentMap.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object value = fieldAttachmentMap.get(key);
//Map.Entry entry = (Map.Entry) iter.next();
fieldAttachmentInfo.append(value + ":");
fieldAttachmentInfo.append(key + "|");
[ November 17, 2005: Message edited by: Priya Singh ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what was your problem with entrySet()? This should have worked perfectly:

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's go discuss this in Java in General (Intermediate).
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I love it when people say their code doesn't work, but won't show you the code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic