Search code examples
javadropwizard

How to fix java.lang.ClassCastException:.Review are in unnamed module of loader 'app'


I made some changes to a particular class Helpers.java and anytime I try to it a particular endpoint I get this error message. The stack trace points down to the Helpers' class.

java.lang.ClassCastException: class com.clever.bas.model.Transaction cannot be cast to class com.clever.bas.model.Review (com.clever.bas.model.Transaction and com.clever.bas.model.Review are in unnamed module of loader 'app')

Here is what the Helpers class look like

public static InputStream writeToExcel(CompletableFuture<ArrayList<Review>> data) throws Exception {

        //Blank workbook
        try (XSSFWorkbook workbook = new XSSFWorkbook()) {

            //Create a blank sheet
            XSSFSheet sheet = workbook.createSheet("transaction report");
            AtomicInteger rowNum = new AtomicInteger(1);
            AtomicBoolean isHeaderSet = new AtomicBoolean(false);
            data.get().forEach(review -> {
                if (!isHeaderSet.get()){
                    createHeader(review, sheet);
                    isHeaderSet.set(true);
                }
                XSSFRow row = sheet.createRow(rowNum.getAndIncrement());
                ObjectMapper mapObject = new ObjectMapper();
                Map<String, Object> mapObj = mapObject.convertValue(review, Map.class);
                AtomicInteger cellNum = new AtomicInteger();
                mapObj.forEach((key, value) -> {
                    XSSFCell cell = row.createCell(cellNum.getAndIncrement());
                    cell.setCellValue(key);
                    if (value instanceof Integer)
                        cell.setCellValue((Integer) value);
                    else if (value instanceof BigDecimal)
                        cell.setCellValue(((BigDecimal) value).doubleValue());
                    else if (value instanceof Long)
                        cell.setCellValue((Long) value);
                    else if (value instanceof Boolean)
                        cell.setCellValue((Boolean) value);
                    else cell.setCellValue((String) value);

                });
            });
            try {
                //Write the workbook in file system
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                workbook.write(bos);
                byte[] barray = bos.toByteArray();
                InputStream is = new ByteArrayInputStream(barray);
//                InputStream is = new FileInputStream("transaction.xlsx");
//                workbook.write(is);
                System.out.println("transaction.xlsx written successfully on disk.");
                return is;
//                out.close();
            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception(e.getMessage());
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());

        }
    }
private static void createHeader(Review review, XSSFSheet sheet) {
        XSSFRow row = sheet.createRow(0);
        ObjectMapper mapObject = new ObjectMapper();
        Map<String, Object> mapObj = mapObject.convertValue(review, Map.class);
        AtomicInteger cellNum = new AtomicInteger();
        mapObj.forEach((key, value) -> {
            XSSFCell cell = row.createCell(cellNum.getAndIncrement());
            cell.setCellValue(key);
        });
    }

I can't think of were I am returning type of Transaction instead of review. Here is where I am calling the helper class in my Service.

 public InputStream downloadReviewReport(UriInfo uriInfo) throws Exception {
        MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
        String userID = queryParams.getFirst("userID");
        CompletableFuture<ArrayList<Review>> reviews = reviewRepository.fetchUserDetailsBatches(filterTransaction(queryParams, userID));
        return Helpers.writeToExcel(reviews);
    }

Solution

  • There was a place I kept calling a different class in my database configuration. Changing it to the appropriate class type solved the problem