I am using the Quarkus framework and have an ApplicationScoped
bean that implements an interface with a method with the signature of loadRatingByUserIdAndVehicleId(userId: Long, vehicleId: Long): Rating?
.
The implementation of the method for the sake of simplicity may be:
override fun loadRatingByUserIdAndVehicleId(userId: Long, vehicleId: Long) : Rating? = null
However, when I invoke this method, instead of getting null
, I receive the following error message:
2023-06-16 10:40:43,566 ERROR [ExceptionHandler] (executor-thread-1) java.lang.ClassCastException: class kotlin.Unit cannot be cast to class com.example.model.Rating (kotlin.Unit is in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @470a696f; com.example.model.Rating is in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @5e5beb8a)
at com.example.service.RatingService_Subclass.loadRatingByUserIdAndVehicleId(Unknown Source)
at com.example.service.RatingService_ClientProxy.loadRatingByUserIdAndVehicleId(Unknown Source)
The actual implementation of the method should be:
override fun loadRatingByUserIdAndVehicleId(userId: Long, vehicleId: Long) =
ratingRepository.findByUserIdAndVehicleId(userId, vehicleId)
?.let { ratingMapper.toBusiness(it, rating, context()) }
And the implementation of findByUserIdAndVehicleId
is:
fun findByUserIdAndVehicleId(userId: Long, vehicleId: Long): Rating? {
return list("userId = ?1 and vehicleId = ?2", userId, vehicleId).firstOrNull()
}
If the Rating
cannot be found by userId
and vehicleId
, then null
is returned. Otherwise, it is converted. Do you have any idea why this error might occur?
EDIT: For demonstration, click here.