简体   繁体   English

Java中BufferedReader的偶然错误

[英]An occasional error with BufferedReader in Java

I get an ArrayIndexOutOfBoundsException when I invoke this function, but not always. 当我调用此函数时,会得到ArrayIndexOutOfBoundsException ,但并非总是如此。 Out of 10 attempts , I got an error 3 times , and the remaining 7 times it worked perfectly fine. 10次​​尝试中 ,我收到3次错误,其余7次完全正常。

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. 215行恰好是if条件本身。

Even after the change, the error persists. 即使更改后,错误仍然存​​在。

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]);
    }

I get an Array IndexOutOfBoundsException when I invoke this function, but not always. 当我调用此函数时,会得到一个Array IndexOutOfBoundsException,但并非总是如此。 Out of 10 attempts, I got an error 3 times, and the remaining 7 times it worked perfectly fine. 在10次尝试中,我出现3次错误,其余7次则完全正常。

First potential danger : 第一个潜在危险:

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

Here you are assuming coords array will always have 2 elements . 在这里,您假设coords数组将始终具有2元素。

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 . 您的xy可能不包含i的索引。

Show us your Model class , specifically the attributes x and y and how do you intialise them. 向我们展示您的Model类,特别是属性xy以及如何初始化它们。

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. 抛出IndexOutOfBoundsException以指示某种索引(例如,数组,字符串或向量)超出范围。

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. 您应该在访问之前检查它的长度coords.length

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. 您假设您将始终在coords中包含至少两个元素的数组,并添加一些代码以验证您确实得到了至少两个元素的数组。

Change your if statement to: 将您的if语句更改为:

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

First check the length of the coords array then move forward as 首先检查coords数组的长度,然后按

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. 由于未检查长度,您假设coords长度始终为2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM