3

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!

5
  • can you check if (String name) is null? Commented Jan 31, 2013 at 18:03
  • Yeah, I checked that and it isn't Commented Jan 31, 2013 at 18:06
  • just for fun, could you post your LogCat? Commented Jan 31, 2013 at 18:15
  • Put a breakpoint on that line. Run your code until it hits the breakpoint. Inspect all of the objects. Which one is null? Commented Jan 31, 2013 at 18:24
  • At the breakpoint the variables were this, db, values, name, and n, none of which were null. I don't think that I have some null variables, I believe the issue is that db is not open, for some reason. Commented Jan 31, 2013 at 19:02

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

This class actually is nested. In any case, why does this work for changeName() and not changeSize()? That is the real mystery. There is also a method called changePassword() which is almost identical and works as well.
Do you call your methods in same sequence? If yes, try to change it, try to rebulid project, try to use debugger, try do not close db after first call.
I have called the methods in different order, cleaned project and used debugger. Not closing db after the first call did not solve the problem either.
I rewrote my database code to more closely resemble the code in the link you sent. I still don't know why my original code wasn't working, but I'll move on before I've pulled all my hair out! Thanks!
1

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.

1 Comment

Actually, following Georgy Gobozov's suggestion to rewrite my database code in the way described by the link he provided, I have made those changes. This seems to have solved the problem (if not the mystery), but I'll accept his answer on that account. Thanks for the help though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.