tags:

views:

40

answers:

2

Hi Guys,

I'm trying to write a function that will delete every row in a given table but I'm getting a null pointer exception. Could somebody point me in the right direction? Here is the code...

   public void deleteall(){
    SQLiteDatabase db = tweets.getWritableDatabase();
    String delete = "TRUNCATE FROM tweets";
    db.rawQuery(delete, null);

      }
+1  A: 

Check if tweets is null.

I think it's more simpler to use this call, than using rawQuery. Your rawQuery must be parsed, but using the delete method it uses already a parametrized query.

db.delete('tweets',null,null);
Pentium10
Still getting a nullpointer, I'm using the count method you helped me with in another question to check if rows > 1 if it is then I'm executing that method. But I'm still getting a null pointer, I assume I'm going about this the wrong way.. is their a better way to check for a null table?
Ulkmun
Check the `adb logcat` or `ddms` to see what causes the null pointer. I think you have some other stuff null there. http://www.brighthub.com/mobile/google-android/articles/25023.aspx
Pentium10
A: 

SQLite does not have any TRUNCATE statement, so you must do:

public void deleteall(){
    SQLiteDatabase db = tweets.getWritableDatabase();
    String delete = "DELETE FROM tweets";
    db.rawQuery(delete, null);
}
Cristian