Announcement Announcement Module
Collapse
No announcement yet.
ClassCastException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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>>

    Comment


    • #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) {

      try{

      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.

      Comment


      • #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.

        Comment


        • #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.

          Comment


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

            Comment

            Working...
            X