while building a simple currency converter app in android kotlin I got the following error at * operator ? what could be reason for the error.
CodePudding user response:
Seems that rate
variable is nullable (float?
I suppose), while multiplying operation work with non null values only.
Also smart cast wasn't used here, so rate
variable seems to be changeable. So I recommend to make it unchangeable (val
variable), or use buffer variable like following:
val bufferRate = rate
if(bufferRate == null) {
...
} else {
val convertedCurrency = fromAmount * bufferRate //here `bufferRate` should be smart casted to non nullable type
}
CodePudding user response:
You cannot use round() function with nullable Any and Float variables. Also you need to convert Any to Float. Try to convert them with this example:
var rate : Any? = 5
var fromAmount : Float? = 3.5f
val result = round(rate!!.toString().toFloat() * fromAmount!!)