1

I have the following code:

List<Details> detailsList = new ArrayList<>();
List<String[]> csv = csvReader.readAll();
final Map<String, Integer> mappedHeaders = mapHeaders(csv.get(0));
List<String[]> data = csv.subList(1, csv.size());
for (String[] entry : data) {
    Details details = new Details(
        entry[mappedHeaders.get("A")],
        entry[mappedHeaders.get("B")],
        entry[mappedHeaders.get("C")]);
detailsList.add(details);

I'm essentially reading in a CSV file as a list of string arrays where the first list item is the CSV file headers and all remaining elements correspond to the data rows. However, since different CSV files of the same features might have different feature column ordering I don't know the ordering in advance. For that, I have a mapHeaders method which maps the headers to indices so I can later properly put together the Details object (for example, if headers are ["B", "A", "C"], the mappedHeaders would correspond to {B: 0; A: 1; C: 2}.

I also have some test data files of different column orderings and all but one of them work as they should. However, the one that doesn't work gives me

java.lang.NullPointerException: cannot unbox null value

when trying to evaluate entry[mappedHeaders.get("A")]. Additionally, when running the code in debugging mode, the mappedHeaders contains the correct keys and values and the value for "A" isn't null.

I have also tried entry[mappedHeaders.getOrDefault("A", Arrays.asList(csv.get(0)).indexOf("A"))] which returns -1. The only thing that works is entry[mappedHeaders.getOrDefault("A", 0)] since A is the first column in the failing case, but that workaround don't seem very feasible as there might be more failing cases that I don't know about, but where the ordering is different. What might be the reason for such behavior? Might it be some weird encoding issue?

live-love
  • 48,840
  • 22
  • 240
  • 204
C. R.
  • 21
  • 2
  • Check [this post](https://stackoverflow.com/questions/2382058/unboxing-null-object-to-primitive-type-results-in-nullpointerexception-fine)! – Mike B Mar 01 '21 at 13:47
  • Does this answer your question? [Unboxing Null-Object to primitive type results in NullPointerException, fine?](https://stackoverflow.com/questions/2382058/unboxing-null-object-to-primitive-type-results-in-nullpointerexception-fine) – Mike B Mar 01 '21 at 13:48
  • I would definitively check the encoding or even file access, since reading other files is working. Maybe also log or print map content after reading –  Mar 01 '21 at 13:52
  • Seems like I figured it out. The failing case had UTF-8 with BOM encoding and skipping the characters seems to work so far. – C. R. Mar 01 '21 at 13:55

1 Answers1

1

That's because you are trying to unbox a null value.

A method like intValue, longValue() or doubleValue() is being called on a null object.

Integer val = null;
    if (val == 1) {
    // NullPointerException 
    }

Integer val = null;
    if (val == null) {
    // This works
    }

Integer val = 0;
    if (val == 1) {
    // This works
    }
live-love
  • 48,840
  • 22
  • 240
  • 204