实现类中的ClassCastException

时间:2012-10-30 18:40:10

标签: java spring hibernate classcastexception

为什么我得到ClassCastException:

包装异常:

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)

我的实现类是

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;
    }

我的行动电话是:

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

我在第68行得到错误,即“Object [] objs =(Object [])it.next();”线。 为什么我会遇到这个问题?

任何输入......今天重新审视此问题并面对问题。

3 个答案:

答案 0 :(得分:3)

我假设,您findAll查询是检索所有CasePmt个对象。在这种情况下,我认为你有以下问题:

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

,因为您的迭代器将返回CasePmt个对象。

尝试将其强制转换为CasePmt对象:

 CasePmt casePmt = (CasePmt)it.next();

答案 1 :(得分:1)

通常,只要你有一个ClassCastException,你就不明白了,而不是

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

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

答案 2 :(得分:0)

将您的列表声明为

  List<Object[]> list;  

如果要获取对象数组,只需使用it.next()而不是

 (Object[]) it.next();