java.lang.String不能强制转换为java.lang.Integer JAVA Hibernate

问题描述

像这样将我的集合提交到数据库后,我出错了

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()在这种情况下)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...