Dnysus
Dnysus

Reputation: 35

Name expected in kotlin

I am getting an error and im not sure why, I've tried a few different solutions ive found online and nothing has worked.

@Database(entities = {Tea.class}, version = 1, exportSchema = false)

the error im getting is that "Name expected" for after {Tea/class}

package com.google.developers.teacup.data

import android.content.Context
import androidx.room.Database
import androidx.room.Entity
import androidx.room.RoomDatabase
@Entity
@Database(entities = {Tea.class}, version = 1, exportSchema = 
false)
abstract class TeaDatabase: RoomDatabase() {

    abstract fun teaDao(): TeaDao

    companion object {

    @Volatile
    private var instance: TeaDatabase? = null

    /**
     * Returns an instance of Room Database
     *
     * @param context application context
     * @return The singleton TeaDatabase
     */
    fun getInstance(context: Context): TeaDatabase {

    return(instance!!)}
    }
}

Upvotes: 3

Views: 2343

Answers (2)

tymwitko
tymwitko

Reputation: 33

Entities is supposed to be an array, so in Kotlin it should be defined with [], meaning your @Database line should be:

@Database(entities = [Tea::class], version = 1, exportSchema = false)

(bearing in mind what Shailendra Madda said about using Tea::class instead of Tea.class)

Upvotes: 1

Shailendra Madda
Shailendra Madda

Reputation: 21561

As per Kotlin syntax, you need to replace Tea.class with Tea::class

@Database(entities = {Tea::class}, version = 1, exportSchema = false)

Remove @Entity from your TeaDatabase class and add it to Tea.class.

Hope it should solve your problem.

Upvotes: 3

Related Questions