先上代码:
这里是Action中的一段代码
- Java code
……List<CustomerDevote> customerDevoteList = customerDevoteService.getList();for(CustomerDevote cd : customerDevoteList) //line1{ System.out.println(cd.getCustomerName());}……
line1这一句报错:
java.lang.ClassCastException: com.…….model.Customer cannot be cast to com.…….model.CustomerDevote
我很无语。这是为什么啊?明明返回的就是CustomerDevote泛型,为什么它会报这样的错?要怎么解决?
------解决方案--------------------
你在添加元素的时候添加的是什么类型,最好能把添加的那一段代码贴下
------解决方案--------------------
------解决方案--------------------
public List<CustomerDevote> getList()
{
List<Customer> list1 = baseDao.getList(obj);
List<CustomerDevote> list2 = new ArrayList<CustomerDevote>();
int sum = 0;
for(Customer c : list1)
{
CustomerDevote cd = new CustomerDevote();//为什么不放在里面,你那个应该是只添加了一个元素,但是也应该不会出现转型问题。。有没调错函数
sum = 0;
for(Order o : c.getOrders())
{
sum += o.getTotalPrice();
}
cd.setCustomerId(c.getId());
cd.setCustomerName(c.getName());
cd.setOrderCount(c.getOrders().size());
cd.setTotalMoney(sum);
list2.add(cd);
}
return list2;
}