更新方法返回null

时间:2018-11-17 12:08:56

标签: java android sql database sqlite

使用以下代码

SQLiteDatabase db;

MainData myDBHlpr = new MainData(getActivity());

String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";
Log.d("Tager", MMS);
db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + " WHERE Data = MyData");

我在db.execSQL行上遇到错误...有人告诉我这是因为未初始化db,但我还应该怎么办?

这是错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference

at com.appmaster.akash.messageplus.Settings.UsageSettings.onCreateView(UsageSettings.java:44)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

1 个答案:

答案 0 :(得分:0)

您必须先打开SQLiteDatabase,然后再查看Android Docs