I got error of ArrayIndexOutOfBounds Exception in following code.

Please help me to how to solve the error.I also used String tokenizer class.

import java.io.*;
import java.lang.*;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.StringTokenizer;

public class CSV{

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fileName="C:/Users/JAL/Desktop/dataset.csv";
        try {
            BufferedReader br = new BufferedReader( new FileReader(fileName));
            String strLine = null;
            StringTokenizer st = null;
            int lineNumber = 0, tokenNumber = 0;
            int row = 0;
            int col = 0;
            String [][] numbers = new String[100][100];

            while( (fileName = br.readLine()) != null)

                //break comma separated line using ","
                st = new StringTokenizer(fileName, ",");

                    //display csv values
                    //System.out.println("Line # " + lineNumber + 
                            //", Token # " + tokenNumber 
                            //+ ", Token : "+ st.nextToken());
                //reset token number
                tokenNumber = 0;

        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block


Recommended Answers

All 7 Replies

What is ur program actually doing??? what is present in the csv file??

If I were to guess where you get your index out of bounds error it would be on line 38. Your initial pass through your 2nd while loop will be fine as you have set your col value to zero but once you finish that 2nd while loop, the value does not set back to zero. It will pass your maximum value of 100 if you have more than 100 tokens.

Please help me to how to solve the error.

  1. The error message contains the line number where the error happened; go to that line.
  2. Immediately before that line add a print statement to print the value(s) of the index variable(s)/expressions(s) to see exactly when they are going out of range.
  3. Back-track through your code to see why they are going out of range. Add more print statements to show the values of key variables to help identify the exact source of the error.


you are right,i got error at line 38.
but i have exactly 100 tokens.but still i got error at this line.
so what to do for that?


in my csv file there is 100 rows containing following data



ans so on..
so now please tell me what to do for removing an error.

do you know what event throws an ArrayIndexOutOfBoundsException?
let's say you have an array of ten ints:

int[] row = new int[10];

the first index for this (and every other) array is 0, the highest possible(last) one is 9 (10-1)

so, if you call

int a = row[x];

with x being an index < 0, or 9 > x => you'll get an ArrayIndexOutOfBoundsException, because you are trying to access a member of the array 'out of the arrays bounds'

commented: thnkas +1

wen_cai's first post tells you exactly what is wrong with your code. Go back and read it again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.