Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type casting problem with java for-each loop

I have traced an issue with an application I am developing, it is giving me a type cast exception. Funny thing is it is saying it cannot cast "entities.Movie cannot be cast to entities.Movie"?! movies is an ArrayList.

    try {
        movies = getMovies();
    } catch (Exception e) {
        e.printStackTrace(System.out);
    } finally {
        try {
            for (Movie movie : movies) {
                output.append("                 <tr>\n");
                output.append("                     <td>" + movie.getId() + "</td>");
                output.append("                 </tr>\n");
            }
         } catch (Exception e) {
             e.printStackTrace(System.out);
         }
     }
like image 229
pharma_joe Avatar asked Dec 23 '22 03:12

pharma_joe


1 Answers

Sounds like a classloader conflict. The same class definition, loaded by different classloaders, is seen as two distinct classes by the JVM.

With this little info, there is not much more to say. See this article for details on classloaders and their problems.

See also this earlier answer of mine to a similar problem.

like image 166
Péter Török Avatar answered Jan 02 '23 14:01

Péter Török