I have a CSV file, looking like this:

```
First, Surname, Age    
Jim, Halpert, 35
Dwight, Schrute, 38

```

And I need to store it by Column (List of First names, list of surnames, list of ages). And I created a list of lists that would store this representation.

```
List<List<String>> myList = new ArrayList<List<String>>();
        while ((row = csvReader.readLine()) != null)
        {
            String[] myRow = row.split(",");
            for (int j=0; j<COLUMN_COUNT; j++)
            {
                myList.get(j).add(i,myRow[j]);
                System.out.println(data.get(j).get(i));

            }
            i++;
        }

```

My question is since I know what COLUMN\_COUNT is, why doesn't this work? I am just trying to go through every line, and in every line, I separate the CSV to an array and then want to store this in a column-based list of lists.

The actual error I am getting is:

```
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

```

and the error happens on the line `myList.get(j)...`

Thanks

***Edit***

Some of you suggested working examples, which now I am using, so thank you very much for that! Now, I am thinking that I might use Java Streams, so it would take less code and be more optimized. However when I do:

```
List<List<String>> myList = Files.lines(Path.of(csvPath)) // Stream<String>
                    //  .skip(1) // (optional) skip a header line
                    .map(line -> Arrays.stream(line.split("\\s*,\\s*")) // split by comma and trim whitespaces
                            .collect(Collectors.toList()) // get list of columns
                    ) // Stream<List<String>>
                    .collect(Collectors.toList());

```

I only get something like this as a result:

```
[Jim, Halpert, 35]
[Dwigth, Schrute, 38]

```

But I actually need

```
[Jim, Dwight]
[Helpert, Schrute]
[35,38]

```

Any suggestions? Thanks!