Note that there are some explanatory texts on larger screens.

plurals
  1. POAn occasional error with BufferedReader in Java
    text
    copied!<p>I get an <code>ArrayIndexOutOfBoundsException</code> when I invoke this function, but not always. Out of <strong>10 attempts</strong>, I got an error <strong>3 times</strong>, and the remaining <strong>7 times</strong> it worked perfectly fine. </p> <pre><code>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 &amp;&amp; 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); } } </code></pre> <p>The error is as follows :</p> <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at polygonprobability.Util.readTrainingData(Util.java:215) </code></pre> <p>I can't debug it because whenever I'm trying to, I don't get the error.</p> <p>I'm thoroughly confused. Please help.</p> <p>EDIT : I changed the conditional to</p> <pre><code>if (coords[0] != null &amp;&amp; coords[1] != null &amp;&amp; coords.length == 2) { m.x[i] = Float.parseFloat(coords[0]); m.y[i] = Float.parseFloat(coords[1]); } </code></pre> <p>Line 215 happens to be the if conditional itself.</p> <p>Even after the change, the error persists.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload