Search code examples
android-studiokotlinandroid-geofence

How to properly initialize geofence list in Kotlin


I'm trying to get geofences working on Android Studio. When debugging, I receive "No geofence has been added to this request."

the code below worked for someone else. I have also tried lateinit var geofencelist : ArrrayList<Geofence> however the error message returns geofencelist not properly initialized.

> lateinit var geofencelist : MutableList<Geofence>

have tried MutableList as well as lateinit and in style below

> private var geofencelist : ArrayList<Geofence>? = null 
> 
> //geofence builder successful geofencelist?.add(geofence) 
> 
> Log.e("D", geofencelist?.count().toString())

output received:

E/D: null Caused by: java.lang.IllegalArgumentException: No geofence has been added to this request.

TYIA


Solution

  • geofencelist?.add(geofence) does nothing if you haven't set geofencelist to something non-null, that's the point of ?..

    It certainly doesn't seem like you should be doing something tricky, since list initialization is trivial; just

    val geofencelist = mutableListOf<Geofence>
    

    then geofencelist.add(geofence) where you had ?.add.