RxJava插入和返回ID方法(房间)调试

时间:2019-07-14 09:19:46

标签: java android android-sqlite rx-java rx-java2

我正在尝试从活动中将行插入SQLITE数据库中,然后将rowId返回到要存储在活动中的变量中。请在下面查看我正在使用的方法和逻辑。

  private void insert() {

    Log.d(TAG, "saveNote: called.");
    try {
       // String automaticThoughtString = automaticThoughtET.getText().toString().trim();
        Note note = new Note(userId, therapistId, "Test", 0, "", "", postedWorkout);
        noteDao.insert(note)
                .map(new Function<Long, Integer>() {
                    @Override
                    public Integer apply(Long aLong) throws Exception {
                        long l = aLong;
                        return (int) l;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(Integer integer) {

                        sqCbtId = integer;
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

android studio中的调试显示:

  W/System.err: java.lang.NullPointerException: Attempt to  invoke interface method 'io.reactivex.Single  com.example.persistence.dao.NoteDao.insert(com.example.persistenc   e.entity.Note)' on a null object reference
  W/System.err:     at  com.example.ui.activities.NoteActivity.insert(NoteActivity.java:1 42)
    at  com.example.ui.activities.NoteActivity.access$000(NoteActivity.java:43)
    at com.example.ui.activities.NoteActivity$1.onClick(NoteActivity.java:129)
    at android.view.View.performClick(View.java:6897)
    at android.view.View$PerformClick.run(View.java:26104)
    at android.os.Handler.handleCallback(Handler.java:789)
    W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:98)
    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)

1 个答案:

答案 0 :(得分:0)

您的noteDao为空,您没有在使用它之前对其进行过初始化。