• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

problem in Hashtable

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am getting ClassCastException even if i am explicitely casting custom object into string ..i want to know the reason why it is happening.
//any implementation of map will throw null pointer exception
import java.util.*;

class student
{
private String id;
public student(){}
public student(String id) {this.id=id;}
}

public class HashTableExample
{
public static void main(String args[])
{
Hashtable h = new Hashtable();

h.put("A","Apple");
h.put(new StringBuffer("p").toString(),"nitesh");
h.put(new student("p").toString(),"naresh");
Set<String> s=h.entrySet();

for(String str:s)
{
String s1=(String)h.get(s);

System.out.println(s1);
}


}
}

give me some suggestions
Thanks
 
author
Posts: 23893
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nisu pathak wrote:i am getting ClassCastException even if i am explicitely casting custom object into string ..i want to know the reason why it is happening.
//any implementation of map will throw null pointer exception



An entry set hold entry objects. Entry objects are not strings objects, and hence, can't be casted to strings.

Henry
 
nisu pathak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have not explicitely defined entry type.but key or entry can be anything .in this case entry should be of type String .even if i type case entry into String it doesn't work .so how it can be possible ?
 
Henry Wong
author
Posts: 23893
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nisu pathak wrote:i have not explicitely defined entry type.but key or entry can be anything .in this case entry should be of type String .even if i type case entry into String it doesn't work .so how it can be possible ?



Here is the javadoc of the entry class...

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

In your code (using my post, since you didn't use code tags), a set of this type is returned in line 19. Later, in line 21, you used the enhanced for loop, which uses an iterator of entries. It is at this line that the assignment fails.


EDIT: in reviewing a bit more, it looks like the assignment at line 19 should fail also, as a Set<Map.Entry<String, String>> is not a Set<String> -- so this line should fail first.

Henry
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nisu pathak wrote:i have not explicitely defined entry type.but key or entry can be anything.


You're confusing the value with the entry. An entry is the combination of a key and a value.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic