java.lang.IllegalArgumentException: A required type converter (class SourceConverter) for ArticlesDao is missing in the database configuration.
I have a crash when compiling (runtime) my application because according to the error displayed, the converter is missing in the configuration of my database. how could I solve this? I need help please.
The source code below:
SourceConverter.kt
@ProvidedTypeConverter
internal class SourceConverter {
@TypeConverter
fun stringToSource(string: String?): SourceEntity = Gson().fromJson(string, SourceEntity::class.java)
@TypeConverter
fun sourceToString(list: SourceEntity?): String = Gson().toJson(list)
}
ArticleEntity.kt
@Entity(tableName = "article")
data class ArticleEntity(
@TypeConverters(SourceConverter::class)
@SerializedName("source")
var source: SourceEntity? = null,
@SerializedName("author")
var author: String? = null,
@SerializedName("title")
var title: String? = null,
@SerializedName("description")
var description: String? = null,
@SerializedName("url")
@NonNull @PrimaryKey var url: String,
@SerializedName("urlToImage")
var urlToImage: String? = null,
@SerializedName("publishedAt")
var publishedAt: String? = null,
@SerializedName("content")
var content: String? = null
)
ArticlesDao.kt
@Dao
interface ArticlesDao {
@Query("select * from article where url = :primaryId")
fun findByPrimaryId(primaryId: String?): ArticleEntity?
@Query("DELETE FROM article WHERE url = :primaryId")
fun deleteByPrimaryId(primaryId: String?): Int
@Query("SELECT * FROM article")
fun getAllArticles(): Flow<List<ArticleEntity>>
@Query("DELETE FROM article")
fun clear()
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(articleEntity: ArticleEntity?): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg articleEntities: ArticleEntity?): LongArray?
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(articleEntityList: List<ArticleEntity?>?): LongArray?
@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(articleEntity: ArticleEntity?): Int
@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(vararg articleEntities: ArticleEntity?): Int
@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(articleEntityList: List<ArticleEntity?>?): Int
@Delete
fun delete(articleEntity: ArticleEntity?): Int
}
AppDatabase.kt
@Database(entities = [ArticleEntity::class, SourceEntity::class], version = 1, exportSchema = false)
@TypeConverters(SourceConverter::class, ArticleConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun getArticlesDao(): ArticlesDao
abstract fun getSourcesDao(): SourcesDao
companion object {
@Synchronized
fun getInstance(context: Context): AppDatabase {
if (sInstance == null) {
sInstance = Room
.databaseBuilder(
context.applicationContext,
AppDatabase::class.java, "infotify.db"
)
.addTypeConverter(SourceConverter::class.java)
.build()
}
return sInstance as AppDatabase
}
}
}
@ProvidedTypeConverter
. Does it work if you remove that annotation and removeaddTypeConverter()
? – Varied