Search code examples
javaandroidopencsv

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 when trying to read csv


I have problem when trying to read the csv file, the error appears like this : java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 . I have tried searching in StackOverflow, the error appears same as above.

Code :

private void readCSVFile(String csvNameFile) {
    try {
        File readFolderCSV = new File(Environment.getExternalStorageDirectory() + "/Download/" + csvNameFile);
        CSVReader csvReader = new CSVReader(new FileReader(readFolderCSV.getAbsoluteFile()));
        String[] nextLine;
        while((nextLine = csvReader.readNext()) != null) {
            Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(MasterUoM.this, "The specified file was not found", Toast.LENGTH_SHORT).show();
    }
}

MyCSVFile :

1


Solution

  • It seems you are trying to fetch column[2] and column[3] & column[4] but csv is not having 5 columns,

     Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
    

    You can check it by printing nextLine.length().