微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

androidx.sqlite.db.framework.FrameworkSQLiteDatabase.inTransaction上的NullPointerException-Android Room

如何解决androidx.sqlite.db.framework.FrameworkSQLiteDatabase.inTransaction上的NullPointerException-Android Room

在单元测试测试项目中,我试图查询实体EndPoint,但出现此错误

java.lang.NullPointerException
    at androidx.sqlite.db.framework.FrameworksqliteDatabase.inTransaction(FrameworksqliteDatabase.java:100)
    at androidx.room.InvalidationTracker.syncTriggers(InvalidationTracker.java:480)
    at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:353)
    at com.mypackged.data.dao.ConfigurationDao_Impl.setUpdateEndPointConfig(ConfigurationDao_Impl.java:120)
    at com.mypackged.business.server.ServicioGeneralTests.test_just_run_please(ServicioGeneralTests.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

这是我正在尝试使用MockitoJUnitRunner测试的代码

    @Test
    public void test_just_run_please() throws InterruptedException {

        assertNotNull(context);
        CIDatabase db = Room.databaseBuilder(context,CIDatabase.class,CIDatabase.DB_NAME)
                .allowMainThreadQueries()
                .build();
        assertNotNull(db);

        ConfigurationDao configdao = db.getConfiguracionDao();
        assertNotNull(configdao);

        EndPointConfig endPointConfig1 = new EndPointConfig();
        endPointConfig1.setUrl("url");
        configdao.setUpdateEndPointConfig(endPointConfig1); // <--- NullPointerException here

        EndPointConfig endPointConfig = configdao.getEndpointConfig();
        assertNull(endPointConfig);


    }

这是实体:

@Entity(tableName = "endpoint_config")
public class EndPointConfig {


    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private int id;

    @ColumnInfo(name = "url")
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

道:

@Dao
public interface ConfigurationDao {

    @Insert(entity = EndPointConfig.class,onConflict = OnConflictStrategy.ABORT)
    void setUpdateEndPointConfig(EndPointConfig endPointConfig);
    // .. more methods
}

数据库

@Database(entities = {TitularCuenta.class,Familiar.class,Parentesco.class,AppConfiguration.class,EndPointConfig.class},version = 2)
public abstract class CIDatabase extends RoomDatabase {
    public static final String DB_NAME = "please_work_database";

    public abstract ConfigurationDao getConfiguracionDao();
    // more dao methods...
}

这里是gradle依赖项:

    implementation "android.arch.persistence.room:runtime:2.2.4"
    implementation "android.arch.persistence.room:compiler:2.2.4"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "android.arch.persistence.room:compiler:1.0.0"

如果我导航到androidx.sqlite.db.framework.FrameworksqliteDatabase.inTransaction方法,则这里是引发错误的行:

@Override
public boolean inTransaction() {
    return mDelegate.inTransaction();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。