Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Pointer exception when attempting to update row of sqlite database in android

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!

like image 644
Rookatu Avatar asked Nov 25 '25 07:11

Rookatu


1 Answers

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

like image 53
Georgy Gobozov Avatar answered Nov 27 '25 21:11

Georgy Gobozov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!