Android中的Realm null对象引用

时间:2018-04-18 08:01:35

标签: java android database realm

我在Android中遇到Realm这个问题。

完整的项目可以在这里找到" https://github.com/nabjitu/BikeShareW7" 日期:18.04.18

当我尝试添加骑行并随后结束此骑行时,应用程序崩溃。

从错误消息中我觉得这个错误试着告诉我Realm数据库是空的。但我不认为这是因为应用程序设法打印以下两个语句,而最后一个只有在数据插入到Realm数据库中时才打印。

V /数据库:数据插入 V /数据库:数据已插入

有谁能看到我在这里做错了什么? 提前谢谢。

以下是错误告诉我的内容:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nataliebrammerjensen.bikesharemlk/com.nataliebrammerjensen.bikesharemlk.EndRideActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.nataliebrammerjensen.bikesharemlk.database.Ride.getId()' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.nataliebrammerjensen.bikesharemlk.database.Ride.getId()' on a null object reference

以下是我收到的完整错误消息:

04-18 09:38:15.761 4833-4833/com.nataliebrammerjensen.bikesharemlk E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.nataliebrammerjensen.bikesharemlk, PID: 4833
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nataliebrammerjensen.bikesharemlk/com.nataliebrammerjensen.bikesharemlk.EndRideActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.nataliebrammerjensen.bikesharemlk.database.Ride.getId()' on a null object reference
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.nataliebrammerjensen.bikesharemlk.database.Ride.getId()' on a null object reference
                                                                                         at com.nataliebrammerjensen.bikesharemlk.RealmDBStuff$7.execute(RealmDBStuff.java:145)
                                                                                         at io.realm.Realm.executeTransaction(Realm.java:1405)
                                                                                         at com.nataliebrammerjensen.bikesharemlk.RealmDBStuff.getLastAdded(RealmDBStuff.java:139)
                                                                                         at com.nataliebrammerjensen.bikesharemlk.EndFragment.updateUI(EndFragment.java:83)
                                                                                         at com.nataliebrammerjensen.bikesharemlk.EndFragment.onCreateView(EndFragment.java:45)
                                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
                                                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                                                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
                                                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
                                                                                         at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
                                                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
                                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231)
                                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181)
                                                                                         at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
                                                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572)
                                                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                                                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
                                                                                         at android.app.Activity.performStart(Activity.java:6992)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                         at android.os.Looper.loop(Looper.java:164) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

我找到了一种解决方法。以下方法由于某种原因起作用。

//Works
public void writeToDB(final String bikeName, final String startRide, 
final String endRide){
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            Ride ride = bgRealm.createObject(Ride.class);
            ride.setMbikeName(bikeName);
            ride.setMstartRide(startRide);
            ride.setMendRide(startRide);
        }
    },  new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            //Transaction was a success.
            Log.v("Database", "Data Inserted");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            //TRansaction failed and was automaticcaly cancelled
            Log.e("Database", error.getMessage());

        }
    });
}