问题描述
像这样将我的集合提交到数据库后,我出错了
java.lang.String cannot be cast to java.lang.Integer
而且,我不知道要将此列表解析为整数
carFamilySelectList = new ArrayList<SelectItem>();
List<CarFamily> carFamilyList = carFamilyServiceImpl.selectCarFamilyIdList();
carFamilySelectList.add(new SelectItem("","Select one"));
for(CarFamily as : carFamilyList) {
carFamilySelectList.add(new SelectItem(as.getCarFamilyId(),as.getCarFamilyName()));
}
请帮助!
解决方法
很难确定没有数据库中的哪行代码会引发异常,但是我的猜测是原因是导致问题的第一个列表项:
carFamilySelectList.add(new SelectItem("","Select one"));
我假设CarFamily.getCarFamilyId()
返回一个int,并且SelectItem.getValue()
的返回值被强制转换为某个地方的Integer?第一个列表项是一个SelectItem
,其值为空字符串。尝试改用0(或-1或其他值):
carFamilySelectList.add(new SelectItem(0,"Select one"));
但是将来,请在您的问题中提供更多相关信息(引发异常的行以及至少与您的问题相关的自定义类中的方法的签名...,例如getCarFamilyId()
在这种情况下)。