• 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

ClassCastException

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic