You are currently viewing I keep getting an error in kotlin that says “only safe and non-nullable asserted calls are allowed” and I can’t seem to figure it out

I keep getting an error in kotlin that says “only safe and non-nullable asserted calls are allowed” and I can’t seem to figure it out

Heres the function I have, the purpose is to get the distance between 2 points and so I created the variables lat1-2 and long1-2 to get their respective coordinates in the function.(I have a handler that runs every 3 seconds to update lat and long2)

private fun getDistance(){
fusedLocationProviderClient.lastLocation.addOnCompleteListener { task ->
var location1 = task.result
if(location1 == null){
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest,locationCallback,Looper.myLooper())
var lat1 = location1.latitude
var long1:Double = location1.longitude
}else{
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest,locationCallback,Looper.myLooper())
var lat2:Double = location1.latitude
var long2: Double = location1.longitude
}
}
val distanceResult = FloatArray(1)
Location.distanceBetween(lat1,long1,lat2,lon2,distanceResult)
val distanceInMeters = distanceResult

Distancetxt.text = “Distance traveled: ” + distanceInMeters
}

The main function to get the long, lat, altitude, and speed works. And the functions name is getLocation. So I know the calls work and everything. But when I added the bolded stuff, It wouldnt let me assign the variables.

Hope you can guide me towards the right direction, and thanks in advance!

submitted by /u/MatroixBeats
[link] [comments]