ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:398回复:0

java arraylist iteration having a ClassCastException

楼主#
更多 发布于:2016-05-12 14:42
I have model class: public class RecordSet {
    private DateRange daterange;
    private Master master;
    public DateRange getDaterange() {
        return daterange;
    }
    public void setDaterange(DateRange daterange) {
        this.daterange = daterange;
    }
    public Master getMaster() {
        return master;
    }
    public void setMaster(Master master) {
        this.master = master;
    }
}


public class DateRange {
    private Date ddate;

    public Date getDdate() {
        return ddate;
    }

    public void setDdate(Date ddate) {
        this.ddate = ddate;
    }

}

public class Master  {

    private String empcode;
    private String empname;
    private String dept;


    public String getEmpcode() {
        return empcode;
    }
    public void setEmpcode(String empcode) {
        this.empcode = empcode;
    }
    public String getEmpname() {
        return empname;
    }
    public void setEmpname(String empname) {
        this.empname = empname;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }

}
 I have a statement like this List<RecordSet> list = (List<RecordSet>) query.list();
 when the above statement get's executed I'm gettind the list Objects and Each object holds two items(Master,DateRange)? Could any one How can I get those two model class objects I've tried but facing classcast exception: List<RecordSet> list = (List<RecordSet>) query.list();
            for(int i=0;i<list.size(); i++){
                Object set=list.get(i);
                Master masetr=(Master)set;

                System.out.println(masetr);
                }
 thanksYou can't cast them if they are not realted .
 The class of the object you're casting and the class you're casting it to must be related by inheritance; that is, you can cast an object only to an instance of its class's sub- or superclass-not to any random class. Side note: Casting downward in the class hierarchy is automatic, but casting upward is not.

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部