• 社区居民
  • 忠实会员
  • 原创写手

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;

 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.










喜欢0 评分0