android – Wrapped into a reference object to be modified when captured in a closure-ThrowExceptions

Exception or error:

so I am a little new to Kotlin on android and very confused on what “CLOSURES” are. I have watched a ton of videos as well, but nothing.

I finish initialize the mutualList on line 5, but then I am not able to set values to it within the try/catch block.

Can anyone please help me.

private fun returnArray(url: String) : MutableList<String> {

    Log.d("Alele", "returnArray(url)")

    var arrayList = mutableListOf<String>()

    val jsonObjectRequest = JsonObjectRequest(
            Response.Listener { response ->
                try {

                    Log.d("Alele", "Try block")
                    var jsonObject = response.getJSONObject("response").getJSONArray("docs").getJSONObject(0)

                    Log.d("Alele", jsonObject.toString())

                    var id = jsonObject.getString("id")
                    Log.d("Alele", id.toString())

                    var journal = jsonObject.getString("journal")
                    Log.d("Alele", journal)

                    arrayList[0] = id
                    arrayList[1] = journal

                } catch (e: JSONException) {
                    Log.d("Alele", "catch block")
            Response.ErrorListener {
                Log.d("Alele", it.printStackTrace().toString())
    Log.d("Alele", " $arrayList[0]")
    Log.d("Alele", " $arrayList[1]")

    // add the queue to the request
    return arrayList

This is the log is produces. Everything prints out as expected, however, the lines

                    arrayList[0] = id
                    arrayList[1] = journal

are causing the app to crash and whenever I place the cursor on those lines for any hints I get wrapped into a reference object to be modified when captured in a closure which is pretty confusing for me to understand.

2020-02-21 10:22:48.560 31603-31603/com.hylton.volleyproject D/Alele: returnArray(url)

2020-02-21 10:22:48.563 31603-31603/com.hylton.volleyproject D/Alele:  [][0]

2020-02-21 10:22:48.563 31603-31603/com.hylton.volleyproject D/Alele:  [][1]

2020-02-21 10:22:51.108 31603-31603/com.hylton.volleyproject D/Alele: Try block

2020-02-21 10:22:51.110 31603-31603/com.hylton.volleyproject D/Alele: {"id":"10.1371\/journal.pone.0084896","journal":"PLoS ONE","eissn":"1932-6203","publication_date":"2014-01-17T00:00:00Z","article_type":"Research Article","author_display":["Marcel A. L. M. van Assen","Robbie C. M. van Aert","Michèle B. Nuijten","Jelte M. Wicherts"],"abstract":["Background: De Winter and Happee [1] examined whether science based on selective publishing of significant results may be effective in accurate estimation of population effects, and whether this is even more effective than a science in which all results are published (i.e., a science without publication bias). Based on their simulation study they concluded that “selective publishing yields a more accurate meta-analytic estimation of the true effect than publishing everything, (and that) publishing nonreplicable results while placing null results in the file drawer can be beneficial for the scientific collective” (p.4). Methods and Findings: Using their scenario with a small to medium population effect size, we show that publishing everything is more effective for the scientific collective than selective publishing of significant results. Additionally, we examined a scenario with a null effect, which provides a more dramatic illustration of the superiority of publishing everything over selective publishing. Conclusion: Publishing everything is more effective than only reporting significant outcomes. "],"title_display":"Why Publishing Everything Is More Effective than Selective Publishing of Statistically Significant Results","score":9.342657}

2020-02-21 10:22:51.110 31603-31603/com.hylton.volleyproject D/Alele: 10.1371/journal.pone.0084896

2020-02-21 10:22:51.110 31603-31603/com.hylton.volleyproject D/Alele: PLoS ONE

This is what the crash report prints out

E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.hylton.volleyproject, PID: 569

    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

        at java.util.ArrayList.set(

        at com.hylton.volleyproject.MainActivity$returnArray$jsonObjectRequest$1.onResponse(MainActivity.kt:71)

        at com.hylton.volleyproject.MainActivity$returnArray$jsonObjectRequest$1.onResponse(MainActivity.kt:26)



        at android.os.Handler.handleCallback(

        at android.os.Handler.dispatchMessage(

        at android.os.Looper.loop(


        at java.lang.reflect.Method.invoke(Native Method)



I/Process: Sending signal. PID: 569 SIG: 9

Application terminated.
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *