I am trying to implement Room in my android application. I am trying to get data from API and save them in local database. But when I run the application this error occurred
e: [kapt] An exception occurred: java.lang.IllegalArgumentException: intcannot be converted to an Element
This is my database class:
@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 {
if (INSTANCE == null){
synchronized(this){
INSTANCE = Room.databaseBuilder(
context.applicationContext,
UserDatabase::class.java,
"user_database")
.build()
}
}
return INSTANCE!!
}
}
}
This is my Repository:
class Repository(application: Application) {
private var mUserDao: UserDao
init {
val db = UserDatabase.getDatabase(application)
}
@WorkerThread
fun createUser(user: User) {
mUserDao.createUser(user)
}
@WorkerThread
fun deleteUser(id: Int) {
mUserDao.deleteUser(id)
}
@WorkerThread
fun getUser(id: Int) {
mUserDao.getUser(id)
}
And here is my userDao
@Dao
interface UserDao{
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun createUser(user: User)
@Delete
fun deleteUser(int: Int)
@Query("SELECT * FROM user WHERE user_id = :userID")
fun getUser(userID: Int): LiveData<User>
}
My LogginViewModel:
class LoginViewModel(application: Application) : AndroidViewModel(application) {
var repository: Repository = Repository(application)
fun createUser(user: User) = repository.createUser(user)
}
Also this error get occurred:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin' <30 internal calls> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)<1 internal call> at java.lang.Thread.run(Thread.java:745) Caused by: org.gradle.api.GradleException: Compilation error. See log for more details at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16) at org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)<17 internal calls> ...33 more