SyncAdminsDatabaseHelper

SyncAdminsDatabaseHelper

我想获取上次更新SQLite表的日期。

我有一个DataHelper类。

// 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;


}


在MainAtivty文件上,我得到了关注。

//
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));
        }

    }
}


当我运行查询时,出现以下错误。

尝试在空对象引用上调用虚拟方法'android.database.Cursor com.example.photoapp.SyncAdminsDatabaseHelper.getlastsyncdate()'

--------- 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)


如何获取最后日期表格。

最佳答案

您需要实例化SyncAdminsDatabaseHelper,即当前您有一行

DataHelper SyncAdminsDatabaseHelper;


那只声明SyncAdminsDatabaseHelper将指向DataHelper的实例。但是,它没有任何实例的指示,因此为null。

当您使用SyncAdminsDatabaseHelper = new DataHelper(this)时,假定这是正确的签名(这是使用的典型签名),即在构造实例且因此可以将实例指向/指向时。

因此,在尝试使用SyncAdminsDatabaseHelper = new DataHelper(this)之前,需要对Cursor query = SyncAdminsDatabaseHelper.getlastsyncdate();进行编码。

09-10 05:49