-2

I'm working on Android development using Kotlin, and encountering an issue with a lateinit var named polyline. The error I'm getting suggests that even though I assigned a value to polyline inside a function called from the onCreate, the app is still crashing with the message that polyline has not been initialized.

Pls how do I go about this? Is there something I'm missing?

See the code below. I removed all unneccessary codes for this question

class CustomerMapActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var polyline: String


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCustomerMapBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val pickUpLocation = "Hall 2 Car Park, Benin City, Nigeria"
        val dropOffLocation = "Nadia Bakery, Benin City, Nigeria"
        val apiKey = R.string.google_api_key.toString()

        routesDetails(pickUpLocation, dropOffLocation, apiKey)


    }


    override fun onMapReady(mMap: GoogleMap) {
        
        drawRouteOnMap()

    }


    private fun routesDetails(origin: String, destination: String, apiKey: String){

        val routeService = ServiceBuilder.buildService(RouteService::class.java)

        val requestCall = routeService.getRoutes(origin, destination, apiKey)
        requestCall.enqueue(object: Callback<RoutesResponse> {
            override fun onResponse(call: Call<RoutesResponse>, response: Response<RoutesResponse>) {

                if (response.isSuccessful) {

                    if (routeResponse != null && routeResponse.routes.isNotEmpty()){


                        polyline = response.body()?.routes?.get(0)?.overview_polyline?.points.toString()

                    }else{
                    }
                }
            }

            override fun onFailure(call: Call<RoutesResponse>, t: Throwable) {

            }

        })
    }



    private fun drawRouteOnMap() {
        val decodedPolyline = PolyUtil.decode(polyline)
        
    }

Logcat:

FATAL EXCEPTION: main
                                                                                                    Process: leoh.portcustomer, PID: 24494
                                                                                                    kotlin.UninitializedPropertyAccessException: lateinit property polyline has not been initialized
                                                                                                        at leoh.portcustomer.activities.CustomerMapActivity.drawRouteOnMap(CustomerMapActivity.kt:162)
                                                                                                        at leoh.portcustomer.activities.CustomerMapActivity.onMapReady(CustomerMapActivity.kt:104)
                                                                                                        at com.google.android.gms.maps.zzat.zzb(com.google.android.gms:play-services-maps@@18.1.0:1)
                                                                                                        at com.google.android.gms.maps.internal.zzar.zza(com.google.android.gms:play-services-maps@@18.1.0:6)
                                                                                                        at com.google.android.gms.internal.maps.zzb.onTransact(com.google.android.gms:play-services-maps@@18.1.0:3)
                                                                                                        at android.os.Binder.transact(Binder.java:1043)
                                                                                                        at m.ff.c(:com.google.android.gms.dynamite_mapsdynamite@232617040@23.26.17 (150700-0):8)
                                                                                                        at com.google.maps.api.android.lib6.impl.bg.run(:com.google.android.gms.dynamite_mapsdynamite@232617040@23.26.17 (150700-0):19)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loop(Looper.java:223)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7656)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Tried resolving it but wasnt still working. I'll appreciate your asistance

kosi
  • 1
  • 1

1 Answers1

0

The error is caused because the function drawRouteOnMap() uses the variable polyline before it is initialized, since you declared it lateinit. This means you have to use the function drawRouteOnMap() only after polyline has a value.

In this piece of code you provided, moving / calling again the drawRouteOnMap() immediately after

polyline = response.body()?...

should solve your problem, like this:

val requestCall = routeService.getRoutes(origin, destination, apiKey)
requestCall.enqueue(object: Callback<RoutesResponse> {
    override fun onResponse(call: Call<RoutesResponse>, response: Response<RoutesResponse>) {

        if (response.isSuccessful) {

            if (routeResponse != null && routeResponse.routes.isNotEmpty()){


                polyline = response.body()?.routes?.get(0)?.overview_polyline?.points.toString()

                drawRouteOnMap()
            }else{
                ....

Keep in mind that since there is more code other than this, using the function drawRouteOnMap() in the response could not suit your case. By doing that it may generate others errors.

The documentation helps also checking if a variable is initialized or not, have a look.

If this doesn't help, please let me know and I'll provide more support, happy coding!

ItzDavi
  • 443
  • 4
  • 13