jack jack -4 years ago 90
Java Question

Array index out of bounds except error in Java

I'm trying to read in a CSV file and keep getting the error:

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

at dataExpert.Control.main(Control.java:22)

I've tried the following:

String split_by = ",";
BufferedReader buff_reader = new BufferedReader(new FileReader("file path"));
String line;

while((line = buff_reader.readLine()) != null){
String[] b = line.split(split_by);

Answer Source

This is likely caused by a row with only commas. When you call split(","), trailing empty strings are discarded, which leaves you with an empty array. One solution is to call split(",", -1), which preserves trailing empty strings.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download