Home > OS >  error : Class is referenced as a converter but it does not have any converter methods
error : Class is referenced as a converter but it does not have any converter methods

Time:08-24

here is the converter class , throwing this error: Class is referenced as a converter but it does not have any converter methods. public final class DateConvertors{..

      import androidx.room.TypeConverters
      import java.util.*

       class DateConvertors {

      @TypeConverters
      fun fromDateToLong(value: Date): Long {
         return value.time
       }

     @TypeConverters
      fun fromLongToDate(value: Long): Date {
         return Date(value)
       }
      }

this is database class where I am using that convertor

     @Database(entities = [Contact::class], version = 2)
     @TypeConverters(DateConvertors::class)
   abstract class ContactDatabase : RoomDatabase() {

        abstract fun contactDao(): ContactsDAO

       companion object {

    val migration_1_2 = object : Migration(1, 2) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE contact ADD COLUMN isActive INTEGER NOT NULL 
       DEFAULT(1)")
        }

    }

    @Volatile
    private var INSTANCE: ContactDatabase? = null

    fun getDatabase(context: Context): ContactDatabase {
        if (INSTANCE == null) {
            synchronized(this) {
                INSTANCE = Room.databaseBuilder(
                    context.applicationContext,
                    ContactDatabase::class.java,
                    "contactDB"
                )
                    .addMigrations(migration_1_2).build()
            }


            }
            return INSTANCE!!
         }
     }
  }

CodePudding user response:

It seems that you are using the wrong annotation.

The functions in DateConverters are annotated with @TypeConverters, but you should use @TypeConverter there.

  • Related