Search code examples
javaandroidselectandroid-sqlite

How to get the last timestamp date from sqlite table


I want to get the date when the SQLite table was last updated.

I got a DataHelper Class.

// Get the last sync date form app mysql table.
public Cursor getlastsyncdate() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor query =db.rawQuery("SELECT applastsync_adm FROM app_admins_adm ORDER BY applastsync_adm DESC LIMIT 1",null);

    return query;


}

On the MainAtivty file, I got following.

//
private void AdminsLastSynced() {
    Cursor query = SyncAdminsDatabaseHelper.getlastsyncdate();
    if(query.getCount()== 0){
        toastMessage("no result");
        return;
    }else{
        StringBuffer buffer = new StringBuffer();
        while (query.moveToNext()){
            buffer.append(query.getString(8));
        }

    }
}

When I run the query I get below error.

Attempt to invoke virtual method 'android.database.Cursor com.example.photoapp.SyncAdminsDatabaseHelper.getlastsyncdate()' on a null object reference

--------- beginning of crash
2019-10-16 10:27:20.352 30197-30197/com.example.photoapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.photoapp, PID: 30197
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.photoapp/com.example.photoapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.photoapp.SyncAdminsDatabaseHelper.getlastsyncdate()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.photoapp.SyncAdminsDatabaseHelper.getlastsyncdate()' on a null object reference
        at com.example.photoapp.MainActivity.AdminsLastSynced(MainActivity.java:100)
        at com.example.photoapp.MainActivity.onCreate(MainActivity.java:49)

How to get the last date form table.


Solution

  • You need to instantiate SyncAdminsDatabaseHelper, that is currently you have a line

    DataHelper SyncAdminsDatabaseHelper;
    

    That only declares that SyncAdminsDatabaseHelper will point to an instance of DataHelper. However, it has no indication of what instance and is therefore null.

    When you use SyncAdminsDatabaseHelper = new DataHelper(this), assuming that that is the correct signature (it's the typical signature used), that is when the instance is constructed and the instance can therefore be pointed at/to.

    As such you need to code SyncAdminsDatabaseHelper = new DataHelper(this) before you attempt to use Cursor query = SyncAdminsDatabaseHelper.getlastsyncdate();.