I'm trying to store an enum value in a Room database on Android. However, I'm getting a null pointer exception when the entity is fetched from our API and attempted to be saved to the DB. I've tried annotating both the Database class and my Entity class with the @Typeconverters annotation.
My Entity class looks like this:
@Entity
@TypeConverters(SalesOrderStatusConverter::class)
data class SalesOrder(
@PrimaryKey val id: Int,
...
var salesOrderStatus: SalesOrderStatus,
...
)
With an enum class and converter in the same file:
enum class SalesOrderStatus(value: Int) {
UNPACKED(0),
PACKED(1),
SHIPPED(2),
EXPORT(3)
}
class SalesOrderStatusConverter {
@TypeConverter
fun toSalesOrderStatus(value: Int): SalesOrderStatus = enumValues<SalesOrderStatus>()[value]
@TypeConverter
fun fromSalesOrderStatus(value: SalesOrderStatus): Int = value.ordinal
}
When I navigate to the appropriate fragment in the app, I get the following error message and the app crashes after data is fetched from the API and attempted to be saved:
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter value at com.android.sampleappname.data.model.SalesOrderStatusConverter.fromSalesOrderStatus(Unknown Source:2)
Things work as expected when I change var salesOrderStatus: SalesOrderStatus to var salesOrderStatus: Int.
What am I doing wrong here?