0

Why am I getting the ClassCastException:

Wrapped exception:

java.lang.ClassCastException: com.avt.model.CasePmt
    at com.avt.dao.impl.CasePmtDaoImpl.findAllCasePmt(CasePmtDaoImpl.java:68)
    at com.avt.ViewCasePmtAction.view(ViewCasePmtAction.java:127)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    at gov.pbgc.spectrum.util.SpectrumLookupDispatchAction.execute(SpectrumLookupDispatchAction.java:113)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)    
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

My implementation class is

public List<CasePmt> findAllCasePmt() {
            List list;
            list = getHibernateTemplate().executeFind(new HibernateCallback() {
                    public Object doInHibernate(Session session)
                                    throws HibernateException, SQLException {

                            Query q = session
                                            .getNamedQuery("findAll");
                            return q.list();
                    }
            });
            Iterator it = list.iterator();
            List<CasePmt> l = new ArrayList<CasePmt>();

            CasePmt voObj;
            while (it.hasNext()) {
                    voObj = new CasePmt();

                    Object[] objs = (Object[]) it.next();
                    // System.out.print("\n" + objs[0].toString());

                    if (objs[2] != null) {
                            voObj.setCaseTxNum(objs[1].toString());
                    }


    .......................
                        }
                    l.add(voObj);
            }
            return l;
    }

and my action call is :

List<CasePmt> CsList =       
             CasePmtBo.getCaseDao().findAllCasePmt();

Im getting error in line 68 which is "Object[] objs = (Object[]) it.next();" line. Why am i getting this issue?

Any inputs..im revisiting this today and facing the issue.

3 Answers 3

3

I am assuming, you findAll query is to retrieve all CasePmt objects. In that case, I think you have the issue at line below:

 Object[] objs = (Object[]) it.next();

as your iterator will return CasePmt object.

Try casting it to CasePmt object as blow:

 CasePmt casePmt = (CasePmt)it.next();
Sign up to request clarification or add additional context in comments.

8 Comments

I cast it to CasePmt but still in getting the same exception at the same line #.
@JNPW: Can you please share your findAll query?
Its "Select * from JTL.Case_VW"
and in entity class: @Entity @NamedQueries({ @NamedQuery(name = "findAll", query = "FROM CasePmt") })
@JNPW: Not sure bout your first query, but if you are using FROM CasePmt then it must be CasePmt object. To verify, change this : Object[] objs = (Object[]) it.next(); to Object objs = it.next(); and then print the class name as System.out.println(objs.getClass().getName());
|
1

In general whenever you have a ClassCastException you do not understand, instead of

Object[] objs = (Object[]) it.next();
[...]

try

Object o = it.next();
if (o instanceof Object[])
{
    Object[] objs = (Object[]) o;
    [...]
}
else if (o != null)
{
    System.out.println("Retrieved object of class " + o.getClass());
    // ... or however you want to inspect the class of the object you retrieved
}
else
    // process null value

Comments

0

declayer your list as

  List<Object[]> list;  

if you are getting array of objects and just use it.next() instead of

 (Object[]) it.next();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.