2015-11-09 102 views
3

嗨,我有一個ArrayList哪種泛型類型是Bean類的類型。當我嘗試從ArrayList獲取數據時,我收到一個異常。java.lang.ClassCastException:[Ljava.lang.Object;不能轉換爲com.inrev.segmentation.bean.IRUserSegment

下面是代碼:

ArrayList<IRUserSegment> segments = (ArrayList<IRUserSegment>) userSegmentDAO.getDateWiseCount(org.getOrgId()); 
System.out.println("List size"+segments.size()); 
// IRUserSegment itr=segments.get(1);//getting Iterator from arraylist to traverse elements 
for (int i = 0; i < segments.size(); i++) { 
    IRUserSegment value = segments.get(i); 
    System.out.println("Element: " + value.getCount()); 
} 

DAO代碼 公開名單getDateWiseCount(整數ORGID){

 IRUserSegment usersegment = null; 

     try 
     { 

      q.append("SELECT count(*) as cnt, date(updatedOn) as updatedOnDate FROM t_user_segment WHERE updatedOn >= DATE_SUB(CURRENT_DATE, INTERVAL 15 DAY) group by updatedOnDate;"); 
      SQLQuery query = session.createSQLQuery(q.toString()); 
      segmentList = (List<IRUserSegment>) query.list(); 

      //ArrayList<IRUserSegment> details=(ArrayList<IRUserSegment>) 
      if (segmentList != null && segmentList.size() > 0) 
      { 
       List<Object[]> result = query.list(); 

       for (Object[] object : result) 
       { 
        System.out.println("Count"+object[0].toString()+"date"+object[1].toString()); 
       } 

       return segmentList; 
      } 

     } 

     catch (Exception e) 
     { 
      log.error("", e); 
     } 
     return segmentList; 

    } 
+0

請出示異常堆棧跟蹤,併線把它扔了。我的猜測是'userSegmentDAO.getDateWiseCount()'返回一個不是'IRUserSegment'的事物的List。 – dotvav

+0

這是一個轉換錯誤,函數返回類型不能被轉換爲'(ArrayList )',請把你的函數getDateWiseCount代碼 –

+0

你不能從userSegmentDAO.getDateWiseCount(org.getOrgId() )給Arraylist,如果可能請分享userSegmentDAO.getDateWiseCount()的代碼 –

回答

0

嘗試使用這樣的。

List segments = userSegmentDAO.getDateWiseCount(org.getOrgId()); 
System.out.println("List size"+segments.size()); 
// IRUserSegment itr=segments.get(1);//getting Iterator from arraylist to traverse elements 
for (int i = 0; i < segments.size(); i++) { 
    IRUserSegment value = (IRUserSegment)segments.get(i); 
    System.out.println("Element: " + value.getCount()); 
} 

或您修改方法

public List<IRUserSegment> getDateWiseCount(Integer orgId) {} 

,而不是

public List getDateWiseCount(Integer orgId) {} 
相關問題