I've written a function to perform a database query. I want it to return null
if it cannot fetch any result.
fun getServiceCharge(model: String): ServiceChargeMasterList {
val unique = VideoconApplication.daoSession.serviceChargeMasterListDao.queryBuilder().where(ServiceChargeMasterListDao.Properties.ModelCategory.eq(model)).unique()
if (unique != null)
return unique
else
return null!!
}
It gives me kotlin.KotlinNullPointerException
.
Can anyone tell me how can I solve this?
Just specify your return type as ServiceChargeMasterList?
and return null
. The !!
operator is very ugly to use.
You don't even have to use that if
statement if your unique()
method return and optional (or Java object). In that case, you method could look like this:
fun getServiceCharge(model: String): ServiceChargeMasterList? {
return VideoconApplication.daoSession.serviceChargeMasterListDao.queryBuilder().where(ServiceChargeMasterListDao.Properties.ModelCategory.eq(model)).unique()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With