尝试将数据输入数据库时​​出现空指针异常

时间:2019-06-04 10:35:55

标签: android android-room

我正在尝试使用Room将自定义类qualityReportForm输入数据库。我是一个初学者,我试图从此演示中添加代码: LINK

错误正在存储库中专门在我尝试插入的那一行上出现。

THE DAO

package com.ITC.operatorutility;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;

import java.util.List;

@Dao
public interface qualityReportFormDAO {

    @Insert
    void insert(qualityReportForm reportForm);

    @Query("DELETE FROM quality_report_table")
    void deleteAll();

    @Query("SELECT * from quality_report_table ORDER BY id ASC")
    LiveData<List<qualityReportForm>> getAllAttributes();
}

存储库

package com.ITC.operatorutility;

import android.app.Application;
import android.os.AsyncTask;
import androidx.lifecycle.LiveData;
import java.util.List;

public class qualityReportFormRepo {

    private qualityReportFormDAO mqualityReportFormDAO;
    private LiveData<List<qualityReportForm>> mAllReports;

    qualityReportFormRepo(Application application) {
        qualityReportFormDB db = qualityReportFormDB.getDatabase(application);
        mqualityReportFormDAO = db.qualityReportFormDAO();
        mAllReports = mqualityReportFormDAO.getAllAttributes();
    }

    LiveData<List<qualityReportForm>> getAllReports() {
        return mAllReports;
    }


    public void insert (qualityReportForm report) {
        new insertAsyncTask(mqualityReportFormDAO).execute(report);
    }

    private static class insertAsyncTask extends AsyncTask<qualityReportForm, Void, Void> {

        private qualityReportFormDAO mAsyncTaskDao;

        insertAsyncTask(qualityReportFormDAO dao) {
            mAsyncTaskDao = dao;
        }

        @Override
        protected Void doInBackground(final qualityReportForm... params) {
            mAsyncTaskDao.insert(params[0]);
            return null;
        }
    }
}

错误

Caused by: java.lang.NullPointerException
        at com.ITC.operatorutility.qualityReportFormDAO_Impl$1.bind(qualityReportFormDAO_Impl.java:38)
        at com.ITC.operatorutility.qualityReportFormDAO_Impl$1.bind(qualityReportFormDAO_Impl.java:30)
        at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:63)
        at com.ITC.operatorutility.qualityReportFormDAO_Impl.insert(qualityReportFormDAO_Impl.java:79)
        at com.ITC.operatorutility.qualityReportFormRepo$insertAsyncTask.doInBackground(qualityReportFormRepo.java:41)
        at com.ITC.operatorutility.qualityReportFormRepo$insertAsyncTask.doInBackground(qualityReportFormRepo.java:31)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
        at java.lang.Thread.run(Thread.java:841)

0 个答案:

没有答案