Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException

 
Fiona Healy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use the findAll()method associated with an entity bean which returns a collection. I am then trying to store items in the collection in a vector but getting a classcastexception can anyone help please?


Code:

public Vector findAllReports()
{
Vector vect = new Vector();
//ReportDefinitionRemote remote = home.create();
try{
Collection reports = home.findAll();
System.out.println(reports + "in findAllReports()");
for (Iterator i = reports.iterator(); i.hasNext(); ) {
ReportDefinitionPOJO pojo = (ReportDefinitionPOJO)i.next();
vect.add(pojo);

}
}
catch(Exception e){throw new EJBException(e);}
return vect;

}

Error:

00:22:21,678 ERROR [LogInterceptor] EJBException in method: public abstract java
.util.Vector com.rgs.ejb.reportdefinitionmanager.ReportDefinitionManagerRemote.f
indAllReports() throws java.rmi.RemoteException, causedBy:
java.lang.ClassCastException
at com.rgs.ejb.reportdefinitionmanager.ReportDefinitionManagerBean.findA
llReports(ReportDefinitionManagerBean.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection reports = home.findAll();


Here you need to typecast home.findAll() returned value to Collection type that you are using in findAll() in home interface.

Collection reports = (CollectionTypeinHomeInterface)home.findAll();
 
Fiona Healy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the home interface returning a Collection type already? I tried it anyway and i am still getting the class cast exception?
 
Arul Prasad
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Finder Method returns the Collection of Remote Objects only

in the code

ReportDefinitionRemote remote = home.create();

here the Remote Object is ReportDefinitionRemote

home.findAll() returns the Collection of ReportDefinitionRemote objects only...

you cannot type cast to the other type ReportDefinitionPOJO

i hope this was the reason for error



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!