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

Index out of bounds on DataTable if CSV is missing columns #254

Open
agentgt opened this issue Jul 6, 2022 · 2 comments
Open

Index out of bounds on DataTable if CSV is missing columns #254

agentgt opened this issue Jul 6, 2022 · 2 comments
Labels

Comments

@agentgt
Copy link

agentgt commented Jul 6, 2022

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:87)
	at be.quodlibet.boxable.datatable.DataTable.addCsvToTable(DataTable.java:290)
	at be.quodlibet.boxable.datatable.DataTable.addListToTable(DataTable.java:204)

See

if (line.size() >= i) {
    cellValue = line.get(i)

CSVRecord is zero based indexing so it fails.

Should be line.size() > i

@agentgt
Copy link
Author

agentgt commented Jul 6, 2022

How this came about is because there is some bad attempt of escaping:

	public void addListToTable(List<List> data, Boolean hasHeader) throws IOException {
		char separator = ';';
		if (data == null || data.isEmpty()) {
			return;
		}
		String output = "";
		// Convert Map of arbitrary objects to a csv String
		for (List inputList : data) {
			for (Object v : inputList) {
				String value = v.toString();
				if (value.contains("" + separator)) {
					// surround value with quotes if it contains the escape
					// character
					value = "\"" + value + "\"";
				}
				output += value + separator;
			}
			// remove the last separator
			output = removeLastChar(output);
			output += "\n";
		}
		addCsvToTable(output, hasHeader, separator);
	}

Consequently our current workaround is:

	private static final char BOXABLE_SEPARATOR = ';';
	private static final CSVFormat BOXABLE_FORMATTER 
		= CSVFormat.Builder.create(CSVFormat.EXCEL).setDelimiter(BOXABLE_SEPARATOR).build();
	
	
	static void addListToTable(DataTable t, List<List<?>> data, Boolean hasHeader) throws IOException {
		if (data.isEmpty()) {
			return;
		}
		StringBuilder b = new StringBuilder();
		for (List<?> inputList : data) {
			String line = BOXABLE_FORMATTER.format(inputList.toArray());
			b.append(line).append("\n");
		}
		String output = b.toString();
		t.addCsvToTable(output, hasHeader, BOXABLE_SEPARATOR);
	}

@johnmanko
Copy link
Collaborator

@bhupal4all As soon as we can get 1.7.0 released we can start working on bug fixes. Waiting for @dhorions to review.

@johnmanko johnmanko added the bug label Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants