uniVocity / univocity-parsers

uniVocity-parsers is a suite of extremely fast and reliable parsers for Java. It provides a consistent interface for handling different file formats, and a solid framework for the development of new parsers.
905 stars 249 forks source link

RetryableErrorHandler.handleConversionError throws ArrayIndexOutOfBoundsException #509

Open idahiron opened 2 years ago

idahiron commented 2 years ago

Hello Univocity Team,

It seems that handleConversionError throws an ArrayIndexOutOfBoundsException, when a CSV file contains only one field without commas in a record while the header have seviral columns.

The row[2] fails because of that:

Input CSV:

LastName, FirstName, Number, Date
Johnson, James, 00000, 2022-7-26
Smith

Messages:

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
  at com.univocity.parsers.common.DefaultConversionProcessor.handleConversionError(DefaultConversionProcessor.java:284)
  at com.univocity.parsers.common.DefaultConversionProcessor.validateAllValues(DefaultConversionProcessor.java:173)
  at com.univocity.parsers.common.DefaultConversionProcessor.applyConversions(DefaultConversionProcessor.java:132)
  at com.univocity.parsers.common.processor.core.BeanConversionProcessor.createBean(BeanConversionProcessor.java:663)
  at com.univocity.parsers.common.processor.core.AbstractBeanProcessor.rowProcessed(AbstractBeanProcessor.java:54)
  at com.univocity.parsers.common.Internal.process(Internal.java:30)
  ... 62 common frames omitted

ExceptionHandlerExceptionResolver - Resolved [com.univocity.parsers.common.DataProcessingException: 
Unexpected error processing input row [Smith] using Processor com.univocity.parsers.common.processor.BeanListProcessor.
<EOL>Internal state when error was thrown: row=[Smith]] (AbstractHandlerExceptionResolver.java:208)

Original source: my app encounters an error when it processes row[column] = defaultValue;

protected final boolean handleConversionError(Throwable ex, Object[] row, int column) {
        if (row != null && row.length < column) {
            //expand row so column index won't make error handlers blow up.
            row = Arrays.copyOf(row, column + 1);
        }
        DataProcessingException error = toDataProcessingException(ex, row,  #column);

        if (column > -1 && errorHandler instanceof RetryableErrorHandler) {
            ((RetryableErrorHandler) errorHandler).prepareToRun();
        }

        error.markAsHandled(errorHandler);
        errorHandler.handleError(error, row, context);

        if (column > -1 && errorHandler instanceof RetryableErrorHandler) {
            RetryableErrorHandler retry = ((RetryableErrorHandler) errorHandler);
            Object defaultValue = retry.getDefaultValue();
            row[column] = defaultValue;
            return !retry.isRecordSkipped();
        }
        return false;
    }

univocity-parsers, version: 2.9.1

Thank you for your work on the excellent Univocity library.

lavanya051992 commented 2 years ago

I am having the same issue, if there are less characters in a file, the exception is thrown. Did you find any workaround?