Search code examples
androidkotlinonclickbuttonclick

Illegal state exception OnClick Method


I am new to Kotlin and trying to make the application like taboo. I have 6 text views, one of them is the main word and the others are the forbidden words. When I click the button words are replacing with other words. However, after I click the button several times it gives me an Illegal State Exception.

var tabooList = ArrayList<WordModel>()
lateinit var wordMain : TextView
lateinit var word1 : TextView
lateinit var word2 : TextView
lateinit var word3 : TextView
lateinit var word4 : TextView
lateinit var word5 : TextView

Here are my definitions.

 wordMain = findViewById(R.id.kelimeMain)
    word1 = findViewById(R.id.kelime1)
    word2 = findViewById(R.id.kelime2)
    word3 = findViewById(R.id.kelime3)
    word4 = findViewById(R.id.kelime4)
    word5 = findViewById(R.id.kelime5)


    tabooList.add(WordModel("FİİL","İŞ","OLUŞ","HAREKET","EYLEM","SÖZCÜK"))
    tabooList.add(WordModel("UYAK", "ŞİİR", "DİZE", "BENZERLİK", "KAFİYE", "SES"))

This is my OnCreate Method.

fun randomWord(view: View) {
    var random = (0..tabooList.size).random()

wordMain.text = tabooList[random].anaKelime
    word1.text = tabooList[random].kelime1
    word2.text = tabooList[random].kelime2
    word3.text = tabooList[random].kelime3
    word4.text = tabooList[random].kelime4
    word5.text = tabooList[random].kelime5
}

And, this one is my button's onClick method. It gives me an error on this line firstly wordMain.text = tabooList[random].anaKelime .

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.suatkkrer.taboo_android, PID: 21946
java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
    at android.view.View.performClick(View.java:7350)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
    at android.view.View.performClickInternal(View.java:7327)
    at android.view.View.access$3600(View.java:807)
    at android.view.View$PerformClick.run(View.java:28166)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7464)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
    at android.view.View.performClick(View.java:7350) 
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
    at android.view.View.performClickInternal(View.java:7327) 
    at android.view.View.access$3600(View.java:807) 
    at android.view.View$PerformClick.run(View.java:28166) 
    at android.os.Handler.handleCallback(Handler.java:907) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:216) 
    at android.app.ActivityThread.main(ActivityThread.java:7464) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955) 
 Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.suatkkrer.taboo_android.Activities.TabooActivity.randomWord(TabooActivity.kt:333)

This is the error.


Solution

  • Size returns the total amount of items in your list, so if you have 2 items, then the size will be 2, but arrays are zero based, so you have to add a -1 to your statement:

     var random = (0..tabooList.size-1).random()