Reputation: 1
I need to read data from a text file and save it to database.
I have written this code but it is giving me a runtime error:
FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
My code is:
public void GetFrom_Text() {
String []message=null;
BufferedReader br=null;
String name1, faculty1, deparment1, officeNumber1, email1, phone1;
long e;
try{
String sCurrentLine = null;
br =new BufferedReader(new InputStreamReader(getAssets().open("ab.txt")));
while((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine != null )
message=sCurrentLine.split(",");
faculty1 = message[0];
deparment1 = message[1];
name1 = message[2];
officeNumber1 = message[3];
phone1 = message[4];
email1 = message[5];
e = db2.insertRecord(faculty1, deparment1, name1, officeNumber1, phone1, email1, email1);
}
}
catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (br !=null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
the lines in text file contain data such that:
it,csa,Mohammad Bsoul,it239,4573,[email protected]
I need to split each record and save it to database and i need to see if the data is save it successfully to datbase
Upvotes: 0
Views: 1021
Reputation: 943
Few things:
Upvotes: 1
Reputation: 206
Try set Log.d("Line from file", sCurrentLine)
before split and you can see string for split in LogCat. I think your string miss last ","
Upvotes: 1
Reputation: 38595
One of the lines of your text file is not being split into 6 elements. This causes the exception to happen on this line...
email1=message[5];
...because there is no 5th index. Check your text file and make sure the data is correct.
Upvotes: 1