事件调度期间发生异常:java.lang.ClassCastException

时间:2013-12-10 03:21:48

标签: java oracle classcastexception

我的代码中一直在收到ClassCastException。最初的目标是将Set转换为List,因为refreshDetailVOTable方法只会获得Set。问题可能在于将Set转换为ListrefreshDetailVOTable可能会错误List这就是我收到ClassCastException的原因。有什么想法吗?

public List deleteChildPromotionComponentDetails(ClientContext context, List detailIRsToDelete,
    String emergencyAccessPermission) throws RetekBusinessException {

    List exclusionList = null;
    RpmEvent deleteEvent = buildPromotionComponentDetailsDeleteEvent(emergencyAccessPermission);
    deleteEvent.setTransitionNotificationExceptionFlag(true);
    Set detailBOsToDelete = new HashSet();

    for (Iterator iDetails = detailIRsToDelete.iterator(); iDetails.hasNext();) {
         IdentifiableReference detailIR = (IdentifiableReference) iDetails.next();

         PromotionComponentDetail promotionComponentDetail = (PromotionComponentDetail) getService()
                         .readForUpdate(detailIR);
         Set exclusionSet = promotionComponentDetail.getExceptionsAndExclusions();

         exclusionList = new ArrayList (exclusionSet);

         for(Iterator exclusion = exclusionSet.iterator(); exclusion.hasNext();){
             PromotionComponentDetail exclusionDel = (PromotionComponentDetail) exclusion.next();
             exclusionDel.accept(deleteEvent);
             detailBOsToDelete.add(promotionComponentDetail);
         }

     }
     return exclusionList;
}

public void deleteChildDetails(final List parentComponentDetails)
{
    List list = null;
    try {
        list = getCmlPromotionComponentDetailAppService().deleteChildPromotionComponentDetails(
        ClientContext.getInstance(), parentComponentDetails,
        emergencyPermission.getName());
    } catch (RetekBusinessException e) {
        e.printStackTrace();
    }
    refreshDetailVOTable(list);
}

1 个答案:

答案 0 :(得分:0)

在这里查看泛型教程:

http://docs.oracle.com/javase/tutorial/java/generics/

你做的很简单,所以你只需要看第一部分。你可能不需要深入了解通配符。

猜测发生了什么:你的方法正在接收一个参数List detailIRsToDelete,你从中获得一个迭代器并迭代这样的元素:

for (Iterator iDetails = detailIRsToDelete.iterator(); iDetails.hasNext();) {
    IdentifiableReference detailIR = (IdentifiableReference) iDetails.next();
    ...
}

如果有人打电话给你,不小心将IdentifiableReference以外的内容放入detailIRsToDelete,你就会在循环中的赋值语句中得到ClassCastException。相反,如果声明了list参数

List<IdentifiableReference> detailIRsToDelete

编译器会检查将事物放入此列表的行为,并且错误将发生在编译时添加错误对象的位置,而不是稍后在运行时,就像您遇到的那样。< / p>