Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CsvBeanReader.read() does not reliably detect EOF #48

Open
atnak opened this issue Jul 21, 2017 · 0 comments
Open

CsvBeanReader.read() does not reliably detect EOF #48

atnak opened this issue Jul 21, 2017 · 0 comments

Comments

@atnak
Copy link

atnak commented Jul 21, 2017

CsvBeanReader.read() is documented to return null after the last item.

Instead, it fails attempting to parse an invalid line.

BeanListHandler.load() also suffers from this problem.

How to reproduce

public static class Foo {
    int x;
    public int getX() { return x; }
    public void setX(int x) { this.x = x; }
}

{
    Path file = Files.createTempFile("bean_test", ".csv");

    try (CsvBeanWriter<Foo> beanWriter = CsvBeanWriter.newInstance(
            new CsvWriter(Files.newBufferedWriter(file)), Foo.class)) {
        beanWriter.write(new Foo());
    }

    try (CsvBeanReader<Foo> beanReader = CsvBeanReader.newInstance(
            new CsvReader(Files.newBufferedReader(file)), Foo.class)) {
        System.out.println(beanReader.read().getX());
        System.out.println(beanReader.read());    # java.lang.NumberFormatException: For input string: ""
    }
}

Possible workaround

for (;;) {
    List<String> values = beanReader.readValues());
    if (values == null) {
        break; // EOF
    }

    if (values.size() == 1 && values.get(0).isEmpty()) {
        continue; // empty line
    }
    beanReader.toBean(values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant