Reputation: 157
I have done lots of operations with Sqlite database and now I am creating dynamically db file for different logged in user and the name of db file belongs to logged-in username. and using database file according to their log-in.
I am getting StringIndexOutOfBoundsException when I am trying to get new Instance of that db file. e.g.
DatabaseHelper dbHelper = DatabaseHelper.getInstance(this);
dbHelper.methodOfDatabase(); // Throwing Exception
Here is my DatabaseHelper Class code snippet
public static String DATABASE_NAME = "";
public DatabaseHelper(Context context, String dbNmae) {
super(context, dbNmae, null, DATABASE_VERSION);
this.myContext = context;
DATABASE_NAME = dbNmae;
}
And here is my method to check or create new db file
public String checkOrCreateDB(String dbName) {
if (databaseFilesList.contains(dbName)) {
DatabaseHelper.DATABASE_NAME = dbName;
} else {
DatabaseHelper.getNewDatabase(activity, dbName).getWritableDatabase();
DatabaseHelper.DATABASE_NAME = dbName;
}
return DatabaseHelper.DATABASE_NAME;
}
And finally this method to create new database file.
public static DatabaseHelper getNewDatabase(Context context, String dbName) {
if(mInstance != null)
{
mInstance.close();
}
mInstance = new DatabaseHelper(context, dbName);
return mInstance;
}
Any help will be appreciated
Upvotes: 0
Views: 97
Reputation: 152827
You're passing an empty string ""
to SQLiteOpenHelper
constructor. The code doesn't show exactly where the empty string comes from but that explains this crash.
getDatabaseLocked()
gets invoked when you call to e.g. getWritableDatabase()
and it passes the name to Context#getDatabasePath()
that reads the first character without checking there is a first character at all.
Upvotes: 1