Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Class Cast Exception

807569Jun 29 2006 — edited Jun 29 2006
Hi ,

I get a class cast exception in the following program after one employee object is added to the TreeSet. Plz tell me y?

This is my employee class
public class Employee
{
	String name;
	String empid;
	Employee(String name,String id)
	{
		this.name=name;
		empid=id;
	}
	Employee(String name)
	{
		this.name=name;
	}
	
	public String toString()
	{
		return "Employee Name: " + this.name + " Employee id: " + empid;
	}
}
and this is my class that throws classcastexception
import java.util.*;
class CreateSortedSet
{
	public static void main(String args[])
	{
		TreeSet<Employee> s1=new TreeSet<Employee>();
	
		Employee e1=new Employee("Akshatha");
		Employee e2=new Employee("uu");
		Employee e3=new Employee("cc");
		Employee e4=new Employee("dkshatha");
		Employee e5=new Employee("ekshatha");
		System.out.println("RRRRRRRRRRRRR1");
		s1.add(e1);
		System.out.println("RRRRRRRRRRRRR2");
		s1.add(e3);
			System.out.println("RRRRRRRRRRRRR3");
		s1.add(e5);
		s1.add(e2);
		System.out.println("RRRRRRRRRRRRR4");
		s1.add(e4);
		System.out.println("RRRRRRRRRRRRR5");
		Iterator<Employee> it=s1.iterator();
		System.out.println("RRRRRRRRRRRRR6");
		while(it.hasNext())
		{
		System.out.println(it.next());
	}
		
		

	}
	
}
Thanks,
Akshatha
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 27 2006
Added on Jun 29 2006
3 comments
158 views