Results 1 to 6 of 6

Thread: ClassCastException

  1. #1
    Join Date
    Nov 2009
    Posts
    14

    Default 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. #2
    Join Date
    Feb 2008
    Posts
    488

    Default

    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. #3
    Join Date
    Nov 2009
    Posts
    14

    Default

    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.

  4. #4
    Join Date
    Feb 2008
    Posts
    488

    Default

    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. #5
    Join Date
    Nov 2009
    Posts
    14

    Default

    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. #6
    Join Date
    Nov 2009
    Posts
    14

    Default

    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;
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •