-1

This is what I have done, code looks like below :

CSVReader reader = new CSVReader(new FileReader("fileName.csv"), ',' , '"' , 1); // using openCSV
String[] nextLine; // Declaring the array
while ((nextLine = reader.readNext()) != null) {
    if (nextLine != null) {   
        if(nextLine[10] == null){
            /*CODE*/ // Error at the if condition line.
        }
    }
}
devk
  • 21
  • 1
  • 9

2 Answers2

2

You should first check the length of nextLine, for example:

 if(nextLine.length < 11 || nextLine[10] == null){

The idea is to avoid the evaluation of nextLine[10] in case the array does not have 10 (or more) elements.

The exact logic you'll need might vary a bit, depending on what exactly you are trying to achieve.

amit
  • 175,853
  • 27
  • 231
  • 333
  • @MuhammadSuleman Why? It's an or. If the length of the array is smaller than 10, you won't evaluate the 2nd operand. You enter the if condidion if the array is too small, or if `nextLine[10]` is null. As I said, the logic might be varied, but it's hard to tell what he really needs from the code he provided... – amit Jun 02 '15 at 11:51
  • OK, then what next ? it seems like he use nextLine[10] further in code thats why he put if condition in it to check its null or not, and when further it will try to get 10th index item it will again got this exception – Muhammad Suleman Jun 02 '15 at 11:54
  • @MuhammadSuleman The code is missing, I have no idea if he uses nextLine[10] or not. I assumed not, because he's checking if it's null... – amit Jun 02 '15 at 11:56
  • but its seems like he will use. and my argument again is that we check object is null when we use it further and make sure further will avoid in case of null – Muhammad Suleman Jun 02 '15 at 11:58
  • @amit ill be scanning lines and it might be null at different indexes, so have added a check at the 10th index for the time being. – devk Jun 02 '15 at 12:32
  • @amit ill be using the 10th index for sure and other index if I find a particular pattern in the CSV, but need a generalized solution to this, where I can handle it. – devk Jun 02 '15 at 12:36
0

You need to post some sample data to show what you are trying to process. If you are only trying to parse 10 items or less then yes I would expect the above code to fail because java is zero based so you would have nextLine[0] through nextLine[9].

Scott Conway
  • 975
  • 7
  • 13