0

I've come across interesting behavior of Kotlin in Android Studio. When I try to log an element of an array dynamically where the index supplied is -1 and the array is empty, the program freezes in the emulator but never throws an error.

Example code:

val myObjects = mutableListOf<MyObject>()
val objIndex = -1
...
Log.d(TAG, "Non-existent element: ${myObjects[objIndex].myValue}") //stuck here, doesn't move past

What causes this seemingly infinite loop or pause in the Kotlin code execution?

Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • Wasn't able to reproduce. Post Kotlin version and which emulator and SDK you use. – Jenea Vranceanu Aug 24 '20 at 05:57
  • It throws `IndexOutOfBoundsException` in my case. Program shouldn't go into infinite loop, can't reproduce. Can you try to use the debugger and determine if that's the line where program gets stuck? – Animesh Sahu Aug 24 '20 at 08:25
  • Sure, I was using SDK version 29 and Kotlin 1.4.0. I know it stopped there because I was logging various steps from multiple parts of the loop and the output just froze. Maybe it was an emulator issue. I didn't dive too much into it and moved my code past the point where it would happen, but I remember the list was empty when the program froze. I guess it wasn't a Kotlin issue. – Mr.Drew Aug 25 '20 at 15:27

0 Answers0