I have a Created three class and want to cast from object to the userPerformanceDetail bean.
I am getting class cast exception.
First class that i have created is
class UserDetail{
int a;
getter;
setter;
}
Second class is
class UserPerformance{
int b;
getter;
setter;
}
and third class that uses above two classes and have getter and setter
class UserPerformanceDetail{
UserDetail userDetail;
UserPerformance userPerformance;
getter;
setter;
}
and in other class i have created method that returns List
So how can I get UserPerformanceDetail from List
can I do
for(Object obj: list){
UserPerformanceDetail userPerformanceDetail = (UserPerformanceDetail)obj
}
above is giving class cast exception
I am getting class cast exception.
one possibility to avoid ClassCastException
is to use instanceof
:
if (obj instanceof UserPerformanceDetail){
UserPerformanceDetail userPerformanceDetail = (UserPerformanceDetail)obj;
}
for(Object obj: list){
if(obj instanceof UserPerformanceDetail) {
UserPerformanceDetail userPerformanceDetail = (UserPerformanceDetail)obj
}
}
The above code should do it. but it will only get you objects that are UserPerformanceDetail
I got the solution of above problem. I checked my code and got that I have duplicate object "UserPerformanceDetail" when I deleted one and rerun the application I got the correct results
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.