简体   繁体   中英

java.lang.String cannot be cast to java.lang.Integer JAVA Hibernate

i got an error after submit my set to database like this

java.lang.String cannot be cast to java.lang.Integer

And, i dont have no idea about to parse this List to 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()));
        }

Please help!

It's hard to say for sure without what line of code in the database(?) is throwing the exception but my guess is that the cause is that first list entry that's causing the problem:

carFamilySelectList.add(new SelectItem("", "Select one"));

I'm assuming that CarFamily.getCarFamilyId() returns an int, and that the return value of SelectItem.getValue() is being cast to an Integer somewhere? That first list entry is a SelectItem with an empty string as its value. Try using 0 (or -1 or whatever) instead:

carFamilySelectList.add(new SelectItem(0, "Select one"));

But in the future, include more relevant information in your question (the line where the exception is thrown and at least the signatures for the methods in your custom class that are relevant to your question... like getCarFamilyId() in this case).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM