Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Pointer exception when attempting to update row of sqlite database in android
    primarykey
    data
    text
    <p>First off, I have looked at all the similar questions that hours of google and SO searches have displayed. No luck.</p> <p>I have a Database Helper class called PropertiesDatabase which extends SQLiteOpenHelper as follows:</p> <pre><code>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(); } } </code></pre> <p>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 </p> <pre><code>db.update(KEY_PROPERTIES_TABLE, values, KEY_NAME + "=?", new String[] {name}); </code></pre> <p>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().</p> <p>I can't understand why one method works fine and the other doesn't. Any help solving that mystery would be appreciated.</p> <p>Thank you for taking the time to read this.</p> <p><strong>Edit:</strong></p> <p>Here is the relevant logcat info:</p> <pre><code>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) </code></pre> <p>Line 767 is, of course, the call to update.</p> <p>Thanks again!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload