Reputation: 1507
First off, I have looked at all the similar questions that hours of google and SO searches have displayed. No luck.
I have a Database Helper class called PropertiesDatabase which extends SQLiteOpenHelper as follows:
private static final class PropertiesDatabase extends SQLiteOpenHelper{
static final int DATABASE_VERSION = 1;
private static final String KEY_PROPERTIES_TABLE = "PropertiesTable";
static final String KEY_ID = "_id";
static final String KEY_NAME = "name";
static final String KEY_SIZE = "size";
static final String KEY_PASSWORD = "password";
static final String [] columnsProperties = new String[] { KEY_NAME,
KEY_SIZE, KEY_PASSWORD};
private static String CREATE_TABLE;
public PropertiesDatabase(Context context) {
super(context, KEY_PROPERTIES_TABLE, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
CREATE_TABLE = "create table "
+ KEY_PROPERTIES_TABLE + "("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null, "
+ KEY_SIZE + " integer,"
+ KEY_PASSWORD + " text" + ");";
db.execSQL(CREATE_TABLE);
}
public void changeName(String oldName, String newName){
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, newName);
db.update(KEY_PROPERTIES_TABLE, values, KEY_NAME + "=?",
new String[] {oldName});
db.close();
}
public void changeSize(String name, int n){
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SIZE, n);
db.update(KEY_PROPERTIES_TABLE, values, KEY_NAME + "=?",
new String[] {name});
db.close();
}
}
There are many more methods in the class, but I've included changeName and changeSize because they are almost identical, but the first works and the second throws a null pointer exception. The logcat says that the call
db.update(KEY_PROPERTIES_TABLE, values, KEY_NAME + "=?", new String[] {name});
In the changeSize() method is the culprit. I checked using db.isOpen() immediately before that call, and the database is not open. It is open, however, before the update call in changeName().
I can't understand why one method works fine and the other doesn't. Any help solving that mystery would be appreciated.
Thank you for taking the time to read this.
Edit:
Here is the relevant logcat info:
01-31 13:37:17.315: E/AndroidRuntime(1978): FATAL EXCEPTION: main
01-31 13:37:17.315: E/AndroidRuntime(1978): java.lang.NullPointerException
01-31 13:37:17.315: E/AndroidRuntime(1978): at
android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
01-31 13:37:17.315: E/AndroidRuntime(1978): at
android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:115)
01-31 13:37:17.315: E/AndroidRuntime(1978): at
android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1825)
01-31 13:37:17.315: E/AndroidRuntime(1978): at
android.database.sqlite.SQLiteDatabase.replace(SQLiteDatabase.java:1744)
01-31 13:37:17.315: E/AndroidRuntime(1978): at
com.LN.AppName.DatabaseHandler$PropertiesDatabase.changeSize(DatabaseHandler.java:767)
Line 767 is, of course, the call to update.
Thanks again!
Upvotes: 3
Views: 1278
Reputation: 2402
First off: Do not use getReadableDatabase()
when you want to write to the database - and updates are writes. Use getWritableDatabase()
instead. Most often this causes no problem, but it can!
Also you normally should hold onto one instance of SQLiteDatabase within your code. See this comment in SQLiteOpenHelper
's code:
// Darn! The user closed the database by calling mDatabase.close().
Create one writable SQLiteDatabase
object when you first use the SQLiteOpenHelper
and use it within all your methods. Take care of your cursors and close the database only, when you are truely done with your database related code.
Upvotes: 1
Reputation: 13731
I think problem is that your class is static, as I know only nested class may be static. Try to use standart method to work with database, smth like this
Upvotes: 1