0

I'm getting this error, here is the code :

    private List<Integer> getMediasToDelete() {
    
            try (Table table = TableFileFactory.fromFile(excelFilePath, 0)) {
                Iterator<TableRow> rows = table.rowsIterator();
                List<String> definitiveMediaToDeleteList;
                String mediaToKeep;
                while (rows.hasNext()) {
                    TableRow row = rows.next();
                    definitiveMediaToDeleteList = row.getCellValueAsList("A", ",");
                    mediaToKeep = row.getCellValue("B", ",");
                    definitiveMediaToDeleteList.remove(mediaToKeep);
                    mediasIdsToDelete.addAll(definitiveMediaToDeleteList);
    
                    List<String> folderIdOfMedia = getService(TransactionService.class).withReadOnlyTransaction(this::getFolderId);
                    **for (String folderId : folderIdOfMedia) {
                        App.getService(FolderService.class).addMedia(Integer.parseInt(folderId), Integer.parseInt(mediaToKeep));
                    }**
    
                    mediaIdDominantByMediaIdToDelete.put(Integer.parseInt(mediaToKeep), definitiveMediaToDeleteList.stream().map(Integer::parseInt).collect(Collectors.toList()));
                }
    
                return mediasIdsToDelete.stream().map(Integer::parseInt).collect(Collectors.toList());
    
            } catch (Exception e) {
                REPORT.error("Error, cannot retrieve the medias ids", e);
                return null;
            }
        }

    private List<String> getFolderId() {
        final String mediasQueryAsString = "SELECT folder_id" +
                " FROM media_in_folder" +
                " WHERE media_id in ( :mediasIdToDelete )";
        final Query mediaQuery = App.getCurrentSession().createSQLQuery(mediasQueryAsString);
        mediaQuery.setParameterList("mediasIdToDelete", mediasIdsToDelete);

        final List<String> folderIdList = QueryHelper.list(mediaQuery);

        if (CollectionUtils.isEmpty(folderIdList)) {
            return Collections.emptyList();
        }
        return folderIdList;
    }

 public static List list(final Query q) { 
    return get().processQuery(q, () -> q.list()); 
}

I got the error at the bold text (the loop), I saw that a lot of questions about this error have been asked and got answered, most of the time using Integer.parseInt(string) is enough but I don't know why i'm still getting it ? How can I fix this ?

EDIT : I added the getFolderId() method

JhinKazama
  • 55
  • 5
  • 3
    `folderIdOfMedia` contains (at least one) `Integer`, whereas the compiler expects it to contain only `String`s (or `null`). This means that you've got some unchecked casting going on, somewhere in the call `getService(TransactionService.class).withReadOnlyTransaction(this::getFolderId)`. Without seeing that code, it's impossible to say why. – Andy Turner Sep 28 '22 at 14:23
  • What's `QueryHelper.list`? – Andy Turner Sep 28 '22 at 14:26
  • I added for you the getFolderId() method that is used in order to fill the folderIdOfMedia list. It is strange that I have one or more Integer in this list – JhinKazama Sep 28 '22 at 14:27
  • 1
    I suspect that `QueryHelper.list` is actually returning a `List`. – Andy Turner Sep 28 '22 at 14:27
  • It actually returns a list : public static List list(final Query q) { return get().processQuery(q, () -> q.list()); } – JhinKazama Sep 28 '22 at 14:30
  • Please enclose that code snippet in backticks so I can see the angle brackets. – Andy Turner Sep 28 '22 at 14:32
  • You are right, I did some debug and I saw that folderIdList is filled by Integer. Well done – JhinKazama Sep 28 '22 at 14:32
  • I edited it for you, in case you still want to see it. So should I cast the return to make sure I have a list of string ? – JhinKazama Sep 28 '22 at 14:36
  • 1
    `public static List list(final Query q) {` returns a raw-typed list. This is disabling type checking, and allows you do do an unchecked cast of the `List` to `List` (and indeed it will be warning you about that unchecked cast). You've got a `List` that happens to contain `Integer`s; you thought you had a `List` where you were parsing all the elements to `int`s. The easiest change would just be to declare `folderIdList` as `List folderIdList`. – Andy Turner Sep 28 '22 at 14:48

0 Answers0