4

I get an error in my Java application when i try to read a column from each line of my csv file

java.lang.ArrayIndexOutOfBoundsException: 1

My code is like this using OpenCSV

    public void insertOjd(String fichierEntree) throws SQLException {
    try {

        CSVReader reader = new CSVReader(new FileReader(fichierEntree));


        String[] nextLine;

            while ((nextLine = reader.readNext()) != null) {
             if (nextLine != null) {

                 System.out.println(nextLine[1]);                   
             }}....
Stim
  • 63
  • 8
  • 1
    Its likely that you have a big line in the file, without any comma on it and so you would have only nextLine[0] created and when you try to refer index 1 you get this error – ring bearer May 11 '15 at 10:28

1 Answers1

0

It's probably an empty line or comment line in the CSV or something like that. The error means that there is no 2nd value on that row (nextLine[1] is the second value). Check your file and print nextLine[0] and you will see the mistake. nextLine[0] will contain the whole row. Make sure you tell openCSV to use the poper separator, escape character etc.

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23