Null Pointer exception when trying to update sqlite database row in android

First, I went through all the similar questions that the google and SO search clock showed. Bad luck.

I have a database helper class called PropertiesDatabase that extends SQLiteOpenHelper like this:

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 included changeName and changeSize because they are almost identical, but the former works and the latter throws a null pointer exception. The logarithm says that the call

db.update(KEY_PROPERTIES_TABLE, values, KEY_NAME + "=?", new String[] {name});

      

The changeSize () method is the culprit. I checked with db.isOpen () just before this call and the database is not open. It is open, however, before the call to update in changeName ().

I don't understand why one method works fine and the other doesn't. Any help solving this mystery would be appreciated.

Thanks for taking the time to read this.

Edit:

Here is the relevant information about logcat:

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, a call to update.

Thanks again!

+3


source to share


2 answers


I think the problem is your class is static as I know that only nested class can be static. Try using a standard method for working with a database like this



+1


source


First: Don't use getReadableDatabase()

when you want to write to the database - and updates are written. Use instead getWritableDatabase()

. Most of the time this is not a problem, but it is possible!

Also you usually have to keep an instance of one SQLiteDatabase in your code. See this comment in SQLiteOpenHelper

code
:



// Darn! The user closed the database by calling mDatabase.close().

Create one writeable object SQLiteDatabase

on first use SQLiteOpenHelper

and use it in all your methods. Take care of your cursors and only close the database when you've actually executed your database related code.

+1


source







All Articles