wrahool
wrahool

Reputation: 1141

An occasional error with BufferedReader in Java

I get an ArrayIndexOutOfBoundsException when I invoke this function, but not always. Out of 10 attempts, I got an error 3 times, and the remaining 7 times it worked perfectly fine.

void readTrainingData(Model m) throws FileNotFoundException  {

    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader("training_data.csv"));
    } catch (IOException ex)    {
        throw new FileNotFoundException();
    }

    String line = "";
    int i = 0;
    int j;
    try {
        while((line = br.readLine()) != null)    {
            String[] coords = line.split(",");
            if (coords[0] != null && coords[1] != null) {
                m.x[i] = Float.parseFloat(coords[0]);
                m.y[i] = Float.parseFloat(coords[1]);
            }
            else    {
                System.out.println("Check training_data.csv");
            }

            j = 0;
            i++;
        }        
    } catch (IOException ex)    {
        System.out.println("Problem reading file");
        throw new RuntimeException(ex);
    }
}

The error is as follows :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at polygonprobability.Util.readTrainingData(Util.java:215)

I can't debug it because whenever I'm trying to, I don't get the error.

I'm thoroughly confused. Please help.

EDIT : I changed the conditional to

if (coords[0] != null && coords[1] != null && coords.length == 2)   {
         m.x[i] = Float.parseFloat(coords[0]);
     m.y[i] = Float.parseFloat(coords[1]);
}

Line 215 happens to be the if conditional itself.

Even after the change, the error persists.

Upvotes: 0

Views: 349

Answers (5)

MayurB
MayurB

Reputation: 3649

Do this changes

    if (coords.length == 2 && coords[0] != null && coords[1] != null )   {
             m.x[i] = Float.parseFloat(coords[0]);
         m.y[i] = Float.parseFloat(coords[1]);
    }

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49372

I get an Array IndexOutOfBoundsException when I invoke this function, but not always. Out of 10 attempts, I got an error 3 times, and the remaining 7 times it worked perfectly fine.

First potential danger :

if (coords[0] != null && coords[1] != null) 

Here you are assuming coords array will always have 2 elements .

Change your condition to :

 // check for coords.length == 2 first inside the if 
 // if the first condition fails , others won't be evaluated for short circuit AND
 if ( coords.length == 2 && coords[0] != null && coords[1] != null)   {
     m.x[i] = Float.parseFloat(coords[0]);
     m.y[i] = Float.parseFloat(coords[1]);
}

Second ,

m.x[i] = Float.parseFloat(coords[0]);
m.y[i] = Float.parseFloat(coords[1]);

Your x and y may not contain the index valued by i.

Show us your Model class , specifically the attributes x and y and how do you intialise them.

Upvotes: 1

morgano
morgano

Reputation: 17422

You're assuming that you will always have an array of at least two elements in coords, add some code to validate that you are indeed getting an array of at least two elements.

Change your if statement to:

if (coords.length >= 2) {
    m.x[i] = Float.parseFloat(coords[0]);
    m.y[i] = Float.parseFloat(coords[1]);
}

Upvotes: 0

Shahrzad
Shahrzad

Reputation: 1072

First check the length of the coords array then move forward as

if(coords.lenght()>=2) {
  //here parse your float
}

it is due to the unchecked length, you are assuming that coords length will be of 2 always.

Upvotes: 0

Ankur Lathi
Ankur Lathi

Reputation: 7836

IndexOutOfBoundsException is thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

In your case i think following line of code is making problem,

 String[] coords = line.split(",");

you should check it's length coords.length before accessing it.

Upvotes: 1

Related Questions