1

I am getting an error while performing the database operation. In my project when I try to access deleteAll() from Dao class ie.e,AuthenticationDaoof Room Database from my test cases I am getting below error

enter image description here

Refer to the below code:

@Dao
public interface AuthenticationDao {

@Insert
void insert(Authentication authentication);

@Query("delete from authentication")
void deleteAll();

@Query("select * from authentication")
Authentication getAuthInformation();
}

AppDatabase.java

 @Database(entities = {Authentication.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase INSTANCE;

public static AppDatabase getAppDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context,
                        AppDatabase.class,
                        "my-database")
                        .allowMainThreadQueries()
                        .build();
    }
    return INSTANCE;
}

public abstract AuthenticationDao authenticationDao();

}

What is the issue? Is it because I am using AppDatabase dbInstance = Room.databaseBuilder(context,AppDatabase.class,"my-database") .allowMainThreadQueries() .build(); to initialize my database object?

If I use AppDatabase dbInstance = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDatabase.class).allowMainThreadQueries().build(); then it works fine.

What could be the reason?

Kavita Patil
  • 1,784
  • 1
  • 17
  • 30
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ken White Jun 18 '19 at 17:59
  • @KenWhite, My question is totally different. The link you have attached is totally related to `What is NullPointerException`. That is not what I am looking for. – Kavita Patil Jun 18 '19 at 18:28

0 Answers0