-1
    public class ReadCSV {
    public static void main(String[] args) {
        List<CsvParserPojo> csvParserPojoList = getCsv("C:\\Users\\test\\Desktop\\csv\\Test.csv");
        for (CsvParserPojo csvParserPojo:csvParserPojoList){
            System.out.println(csvParserPojo);
        }
    }
    private static List<CsvParserPojo> getCsv(String file) {
        List<CsvParserPojo> csvParserPojoList = new ArrayList<>();
        Path pathToFile = Paths.get(file);
        try(BufferedReader br = Files.newBufferedReader(pathToFile)) {
            String row = br.readLine();
            while (row!=null){
                String [] attributes = row.split(".");
                CsvParserPojo csvParserPojo = getOneCsv(attributes);
                csvParserPojoList.add(csvParserPojo);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  csvParserPojoList;
    }
    private static CsvParserPojo getOneCsv(String[] attributes) {
        double total_Gain = Double.parseDouble(attributes[0]);
        String uid = attributes[1];
        double t1 = Double.parseDouble(attributes[2]);
        double t2 = Double.parseDouble(attributes[3]);
        double t3 = Double.parseDouble(attributes[4]);
        double t4 = Double.parseDouble(attributes[5]);
        double przep_ywom_v = Double.parseDouble(attributes[6]);
        boolean connectionControll = Boolean.parseBoolean(attributes[7]);
        CsvParserPojo csvParserPojo = new CsvParserPojo(total_Gain,uid,t1,t2,t3,t4,przep_ywom_v,connectionControll);
        return  csvParserPojo;
    }
}

I have problem with

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at org.example.ReadCSV.getOneCsv(ReadCSV.java:34)
    at org.example.ReadCSV.getCsv(ReadCSV.java:25)
    at org.example.ReadCSV.main(ReadCSV.java:13)"
Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586
KrTest
  • 1
  • It's because the `attributes` in your `getOneCsv` function is empty. – huytc Mar 11 '20 at 09:38
  • the error says that there is no element in some array (I guess `String[] attributes` of getOneCsv, check whatever is in line 34 because the error is in `ReadCSV.java:34` ), so please check it. – Qazi Fahim Farhan Mar 11 '20 at 09:42

2 Answers2

2

Calling row.split(".") is the problem.

String.split() takes a regular expression and . means "any character", so it will split on literally any character and split will discard any trailing empty elements, which will mean the resulting array of that call is always empty, independent of the value of row.

If you want to split on a literal . character you'll need to escape it:

String[] attributes = row.split("\\.");

The double \ is because you need to escape the backslash for strings in general. Note that the C in CSV stands for comma, so splitting on . is probably not intended anyway.

Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586
0

You call getOneCsv(String[] attributes) with an empty array.
Resulting on

double total_Gain = Double.parseDouble(attributes[0]);

having no element at attributes[0] which raises an ArrayIndexOutOfBoundsException because Index 0 is out of bounds in an array of length 0

IQbrod
  • 1,590
  • 1
  • 2
  • 20