2012-10-30 87 views
0

爲什麼我得到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();」線。 爲什麼我會遇到這個問題?

任何inputs..im今天重新審視這個問題,並面臨問題。

回答

3

我假設,你的findAll查詢是檢索所有CasePmt對象。在這種情況下,我認爲你有問題,低於行:

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

爲你的迭代器將返回CasePmt對象。

嘗試鑄造CasePmt對象的打擊:

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

我將它轉換爲CasePmt,但仍然在獲得相同的異常在同一行#。 – JNPW

+0

@JNPW:你可以分享你的findAll查詢嗎? –

+0

其 「SELECT * FROM JTL.Case_VW」 – JNPW

0

declayer您的清單,

List<Object[]> list; 

如果你得到的對象數組,只需使用it.next(),而不是

(Object[]) it.next(); 
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