Hello, I am practicing with the room database and it has been difficult for me to verify if a username and password exist in my database,the problem is that I am having problems with my application as it throws me the following error.(Attempt to invoke virtual method ‘android.content.Context android.content.Context.getApplicationContext()’ on a null object reference)
//User
@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true)
val id : Int ,
val userName: String,
val nameStudent: String,
val grade: String,
val password: String
)
//UserDao
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addUser(user: User)
@Query("SELECT * FROM user_table WHERE userName LIKE :name AND password LIKE:password")
fun readAllData(name: String, password: String): List<User>
}
//UserDataBase
@Database(entities = [User::class], version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: UserDatabase? = null
fun getDatabase(context: Context): UserDatabase{
val tempInstance = INSTANCE
if(tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
UserDatabase::class.java,
"user_database"
).build()
INSTANCE = instance
return instance
}
}
}
}
//UserRepository
class UserRepository(private val userDao: UserDao, name:String,passWord:String ) {
val readAllData: List<User> = userDao.readAllData(name,passWord)
suspend fun adduser(user: User) {
userDao.addUser(user)
}
}
//UserViewModel
class UserViewModel(application: Application,name:String,passWord:String) : AndroidViewModel(application) {
val readAllData: List<User>
private val repository: UserRepository
init {
val userDao = UserDatabase.getDatabase(
application
).userDao()
repository = UserRepository(userDao,name, passWord)
readAllData = repository.readAllData
}
fun addUser(user: User) {
viewModelScope.launch(Dispatchers.IO) {
repository.adduser(user)
}
}
}
//UserViewModelFactory
class MyViewModelFactory(private var application: Application, private var name: String, private var passWord: String) :
ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(UserViewModel::class.java)) {
return UserViewModel(application, name, passWord) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
//mainFragment
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentMainBinding.bind(view)
val name = binding.textInputUser.editText?.text.toString()
val passWord = binding.textInputPassword.editText?.text.toString()
val factory = activity?.application?.let { MyViewModelFactory(it,name, passWord) }
mUserViewModel = factory?.let { ViewModelProvider(this, it).get(UserViewModel::class.java) }!!
val x = context?.let { UserDatabase.getDatabase(it).userDao() }
val y = x?.readAllData(name,passWord)
binding.buttonIngresar.setOnClickListener {
if (y != null) {
findNavController().navigate(R.id.action_mainFragment_to_candidatesFragment)
} else {
Toast.makeText(context, "Usuario o contraseña no existe", Toast.LENGTH_SHORT).show()
}
}
}