-4

I am trying to retrieve record from a particular column in my data base, after writing the finder method in the entitybean, I am getting error from session bean Class Cast Exception: java.lang.String cannot be cast to packagename.PaymentsLocal. Below is the code snippet in my session bean class

ArrayList a = new ArrayList();
Collection col = null;
 try{
    col = plh.findByAllSessions();
    Iterator i = col.iterator();
    while(i.hasNext()){
       pl = (PaymentsLocal)i.next(); 
       a.add(new paymenthelper( pl.getSession()));
    }
  } catch (Exception e) {
     System.out.println("viewing all sessions"+e.getMessage());
  }
 return a;
}
Lucky
  • 16,787
  • 19
  • 117
  • 151

1 Answers1

0

These lines set up an Iterator<String> implicitly, since whatever collection you're returning in plh.findByAllSessions() is a Collection of Strings.

col = plh.findByAllSessions();
Iterator i = col.iterator();

Thus this line tried to force a String to a PaymentsLocal object, which is not possible, causing a ClassCastException.

pl = (PaymentsLocal)i.next();

This is because i.next() returns an object of the type of the collection.

Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28