Announcement Announcement Module
No announcement yet.
ClassCastException Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException

    Hi guys, I'm a new developer in spring batch and when I execute the test in my application I get an exception. java.lang.ClassCastException: java.util.ArrayList the error appears when I terminate the execute the ItemReader and pass to the ItemWrite of package import org.springframework.batch.item.ItemWriter;

    my class Reader:

    public class CategoriaTarifariaItemReader implements ItemReader<List<CategoriaTarifaria>>{

    public List<CategoriaTarifaria> read() throws Exception,
    UnexpectedInputException, ParseException {

    List<CategoriaTarifaria> categoriaTarifas = obterDadosCategoriaTarifa(3);

    return categoriaTarifas;

    Thank's for your attention!!

  • #2
    What type is your ItemWriter expecting? The ItemWriter must always take a List of whatever the ItemReader is returning. So, in that case, your ItemWriter should expect an item of type List<? extends List< CategoriaTarifaria>>


    • #3
      Yeap, you are right, my ItemWrite is:

      public class CategoriaTarifariaItemWrite implements ItemWriter<List<CategoriaTarifaria>>{

      public void write(List<? extends List<CategoriaTarifaria>> items)
      throws Exception {

      List<CategoriaTarifaria> categoriaTarifas = (List<CategoriaTarifaria>)items;
      for (CategoriaTarifaria categoriaTarifaria : categoriaTarifas) {


      categoriaTarifariaFacade.gravarDadosCategoriaTarif a(categoriaTarifaria);

      }catch(CategoriaTarifariaException ex){
      logger.error("Erro to save CategoriaTarifaria.", ex);
      throw ex;



      But when I debug my application the ItemRead not reach the ItemWrite. I don't know what is happening.

      Thank's for reply.


      • #4
        The statement "(List<CategoriaTarifaria>)items" will never work. "items" is of type "List<? extends List<CategoriaTarifaria>> items".

        Also, please use CODE tags when posting code.


        • #5
          Ok. Thank's. I solved my problem reading item by item instand of reading a list, because when I read a list the container put the list inside another list and when I wrote I couldn't converter the list to my type.


          • #6
            The class ItemReader:
            public class CategoriaTarifariaItemReader implements ItemReader<CategoriaTarifaria>
            public CategoriaTarifaria read() throws Exception,
            			UnexpectedInputException, ParseException {
            		CategoriaTarifaria categoriaTarifaria = null;
            		if(categoriaTarifas == null){
            			categoriaTarifas = obterDadosCategoriaTarifa(3);
            			categoriaTarifaria = categoriaTarifas.remove(0);
            		return categoriaTarifaria;