Kotlinx.coroutines: CorourtineScope returns null

Created on 5 Jul 2019  路  6Comments  路  Source: Kotlin/kotlinx.coroutines

When using coroutines for making network calls, I encountered that

private var viewModelJob = Job()
private val coroutineScope: CoroutineScope = CoroutineScope(viewModelJob + Dispatchers.Main)
private fun getMarsRealEstateProperties() {
    if (coroutineScope == null) {
        Log.d("TAG_TAG", "NULL")
    }
}



md5-d6e652e3eccb6493fc8ea8cf94d9ab08



java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter receiver$0

Is this a bug or some problem on my side?

Most helpful comment

you must create your job and coroutineScope before the init block.

All 6 comments

I have the same problem

I solve my problem by doing this

private lateinit var job: Job

    /**
     * Sets the value of the status LiveData to the Mars API status.
     */
    private fun getMarsRealEstateProperties() {

        job = CoroutineScope(Job() + Dispatchers.Main).launch {
            val propretiesDeferred = MarsApi.retrofitService.getProperties()
            try {
                val result = propretiesDeferred.await()
                _response.value = "Success ${result.size}"

            } catch (t: Throwable) {
                _response.value = "Failure ${t?.message}"
            }
        }
    }

    override fun onCleared() {
        super.onCleared()
        job.cancel()
    }

you must create your job and coroutineScope before the init block.

you must create your job and coroutineScope before the init block.

Thanks @casablancaplusplus this was my error.

you must create your job and coroutineScope before the init block.

Thanks @casablancaplusplus, that was my problem.

you must create your job and coroutineScope before the init block.

It solved my problem, but can u explain what was the problem

Was this page helpful?
0 / 5 - 0 ratings