Type converter error on Android Room Database
Asked Answered
T

2

10

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
            }
        }
    }
Thule answered 23/5, 2021 at 14:27 Comment(2)
I have not tried @ProvidedTypeConverter. Does it work if you remove that annotation and remove addTypeConverter()?Varied
@Varied I just solved this exact problem with your idea! Romoved themn and viola! Thanks!!!!Iulus
G
15

There are three ways to add type converter to room database. But you should make sure you using one method only or it will get error:

  1. add annotation at the type converter class header @ProvidedTypeConverter
  2. Room.databaseBuilder.addTypeConverter(ConverterHelper::class)
  3. add annotation at the RoomDatabase class header @TypeConverters(ConverterHelper::class)
Greenery answered 20/2, 2022 at 3:10 Comment(1)
After long research, This really helped..Mccubbin
G
8

Instead of

Room.databaseBuilder.addTypeConverter(ConverterHelper::class)

Use this:

Room.databaseBuilder.addTypeConverter(ConverterHelper())
Gstring answered 8/12, 2022 at 9:28 Comment(1)
Please, use code blocks inside your answerTahmosh

© 2022 - 2024 — McMap. All rights reserved.