I'm in a bit of trouble with a sample program that I am writing.
Here is my code:
public void prePopulateDatabase(SQLiteDatabase db, Context helperContext){
String fileIn = "";
String[] file;
ContentValues row = new ContentValues();
AssetManager am;
InputStream is = null;
am = helperContext.getResources().getAssets();
try {
is = am.open("Matrix.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while((fileIn = br.readLine()) != null){
file = fileIn.split(",");
row.put(KEYWORD, file[0]);
row.put(DURATION, file[1]);
row.put(AMOUNT, file[2]);
row.put(DESCRIPTION, file[3]);
row.put(LEVEL_NAME, file[4]);
row.put(CHOICE_FOR_TWO_1, file[5]);
row.put(CHOICE_FOR_TWO_2, file[6]);
row.put(CHOICE_FOR_FIVE_1, file[7]);
row.put(CHOICE_FOR_FIVE_2, file[8]);
row.put(CHOICE_FOR_FIVE_3, file[9]);
row.put(CHOICE_FOR_FIVE_4, file[10]);
row.put(CHOICE_FOR_FIVE_5, file[11]);
insertToDBViaContentValues(db, row);
}//end while
} catch (Exception e) {
e.printStackTrace();
}//end try catch
}//end fxn
public void insertToDBViaContentValues(SQLiteDatabase db, ContentValues row){
procedure("SiteMatrixMultiLevels: INSERT TO DB > "+row.toString());
db.insert(TABLE_SITES, null, row);
}
public void validate(SQLiteDatabase db){
Cursor c = db.rawQuery("select * from "+TABLE_SITES, null);
if(c != null && c.moveToFirst()){
while(!c.isAfterLast()){
data("VALUE: " + c.getString(0) + "," + c.getString(1) + "," + c.getString(2) + "," + c.getString(3) + "," + c.getString(4) + "," + c.getString(5)
+ "," + c.getString(6) + "," + c.getString(7) + "," + c.getString(8) + "," + c.getString(9) + "," + c.getString(10)
+ "," + c.getString(11) + "," + c.getString(12));
c.moveToNext();
}
}
}
and I am getting an error of
02-03 20:24:04.537: W/System.err(16437): java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
at
row.put(CHOICE_FOR_FIVE_1, file[7]);
The csv file that I am fetching files from has multiple lines but only follows 2 formats. The first 100 are of this format
pkey,keyword,duration,amount,description,A,B,,,,,
while another 100 follow this format
pkey,keyword,duration,amount,description,,,C,D,E,F,G
I am not sure if im getting the error because the first format type only stops at 7 (since no value for C,D,E,F,G is specified.)
Any help would be appreciated. Thanks!