2

I am reading a csv file. One of the requirements is to check if a certain column has a value or not. In this case I want to check the value in array[18]. However, I am getting

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18

Is there any other way to check the array index if it has a value or empty?

My code:

while ((sCurrentLine = br.readLine())!= null) { 

    String[] record = sCurrentLine.split(",");   

    if(record.length > 0){ // checking if the current line is not empty on the file
       if(record[18] != null){ // as per console, I am getting the error in this line
          String readDateRecord = record[18];
          // other processing here
       }
    }
}
8
  • 1
    get the length of the array first and you can avoid IndexOutfBoundsException.Or else catch the ArrayOutofBoundsException Commented Jul 21, 2015 at 5:55
  • 1
    The exception says there is no element at this position in array. Show us the code where you add elements to array. Commented Jul 21, 2015 at 5:55
  • First check the record.length - 1 == 18 Commented Jul 21, 2015 at 5:56
  • Is there a reason you arbitrarily accessed the 18 element? (need more code) Commented Jul 21, 2015 at 5:57
  • I updated the content to provide some codes Commented Jul 21, 2015 at 6:03

6 Answers 6

2

Look, according to JavaSE7:

ArrayIndexOutOfBoundsException thrown to indicate that an array has been accessed with an illegal index. (In your case) The index is greater than or equal to the size of the array.

Means, the index 18 is not legal for array record in your code. Moreover if the array record is null then you will get another exception called NullPointerException.

To overcome your problem, there are many solutions, one can be

//assuming that record is initialized 
if(record.length > 18){
      String readDateRecord = record[18];
      ...
   }
Sign up to request clarification or add additional context in comments.

Comments

0

Here one approach is this

Object array[] = new Object[18];
boolean isempty = true;
for (int i=0; i<arr.length; i++) {
  if (arr[i] != null) {
    isempty = false;
    break;
  }
}

Comments

0

You may try the following code snippet -

int length = record.length;   
if( (n>0 && n<length-1) && record[n] != null ){

   String readDateRecord = record[n];
   //other processing here

}

Comments

0

Size is fixed in array after creation .if you get index beyond of size then it's generated ArrayIndexOutOfBoundException .So first you need to get the size of array then retrive the value from array

 int size=record.length;
 for(int i=0;i<size;i++)
  {
   if(record[i] != null){
     // other processing here
   }
 }

Declare an array with size “n” and access the n-th element. However, as we already mentioned, the indices of an array with size “n”, reside in the interval [0, n – 1].

Comments

0
public static void main (String[] args) {
    Integer [] record = {1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1};
    for(int i = 0; i < record.length; i++)
        if(record[i] == null) {
            System.out.println(i+1 + " position is null");
        }
}

1 Comment

This will simply print "null is null", which seems pretty silly. Better add the index i to that println
-2

If its an arrayList then check if it contains null

array.contains(null));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.