1

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5 at com.example.android.architecture.MainActivity.updateDisplay(MainActivity.kt:59) at com.example.android.architecture.MainActivity.onCreate(MainActivity.kt:44)

changing the index and class view

class MainActivity : AppCompatActivity() {

    private lateinit var dice: IntArray
    private lateinit var headlineText: String

    private val imageViews by lazy {
        arrayOf<ImageView>(findViewById(R.id.die1),
        findViewById(R.id.die1),
        findViewById(R.id.die2),
        findViewById(R.id.die3),
        findViewById(R.id.die4),
            findViewById(R.id.die5)
        )
    }
        private val headline by lazy { findViewById<TextView>(R.id.headline) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        headlineText = savedInstanceState?.getString(HEADLINE_TEXT)
            ?: getString(R.string.welcome)
        dice = savedInstanceState?.getIntArray(DICE_COLLECTION)
            ?: intArrayOf(6,6,6,6,6)

        lifecycle.addObserver(MyLifeCycleObserver())

        fab.setOnClickListener{
            fabClickHandler()}

        updateDisplay()

    }


    private fun fabClickHandler(){
        dice = DiceHelper.rollDice()
        headlineText = DiceHelper.evaluateDice(this,dice)
        updateDisplay()

    }

    private fun updateDisplay() {
        for (i in 0 until imageViews.size) {

            val drawableId = when (dice[i]) {
                1 -> R.drawable.die_1
                2 -> R.drawable.die_2
                3 -> R.drawable.die_3
                4 -> R.drawable.die_4
                5 -> R.drawable.die_5
                6 -> R.drawable.die_6
                else -> R.drawable.die_6
            }
            imageViews[i].setImageResource(drawableId)
        }
        headline.text = headlineText
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        outState?.putString(HEADLINE_TEXT,headlineText)
        outState?.putIntArray(DICE_COLLECTION,dice)
        super.onSaveInstanceState(outState)
    }
}

Unsure why my android application isn't running

a_local_nobody
  • 6,231
  • 5
  • 18
  • 42
  • Could you mark the line number 59 and 44 with a comment where you are getting the exception? – Razib Aug 24 '19 at 16:06
  • 2
    `dice` only has 5 elements, `imageViews` probably more (and that is not (pure) java!!) – user85421 Aug 24 '19 at 16:15
  • that's not java @CarlosHeuberger, the tag should be removed, you're right – a_local_nobody Aug 24 '19 at 16:29
  • @a_local_nobody but that should have been the OP's duty....(only the Java tag was there when my first comment was written) – user85421 Aug 24 '19 at 16:34
  • no I agree, not saying you are wrong, actually just confirming with you that it is not java :) @CarlosHeuberger – a_local_nobody Aug 24 '19 at 16:44
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – a_local_nobody Aug 24 '19 at 16:45

4 Answers4

1

dice is of size 5 where while ImageViews has 6 elements so the loop run 6 times and in the sixth iteration Exception occurs

1

You are initializing your dice array with five elements in case of null savedinstance

"dice = savedInstanceState?.getIntArray(DICE_COLLECTION)
    ?: intArrayOf(6,6,6,6,6)"'

=========================================================== And your Imageviews Array has 6 elements

private val imageViews by lazy {
        arrayOf<ImageView>(findViewById(R.id.die1),
        findViewById(R.id.die1),
        findViewById(R.id.die2),
        findViewById(R.id.die3),
        findViewById(R.id.die4),
            findViewById(R.id.die5)
        )
    }

===================================================== so while you are accessing dice elements in loop running

for (i in 0 until imageViews.size) {
            val drawableId = when (dice[i]) {

It give exception

0

The error is here:

for (i in 0 until imageViews.size) {
            val drawableId = when (dice[i]) {

imageViews has 6 elements.
dice has 5 elements.

dice = savedInstanceState?.getIntArray(DICE_COLLECTION)
            ?: intArrayOf(6,6,6,6,6)

private val imageViews by lazy {
        arrayOf<ImageView>(
          findViewById(R.id.die1),  
          findViewById(R.id.die1),  //maybe it is duplicated
          findViewById(R.id.die2),
          findViewById(R.id.die3),
          findViewById(R.id.die4),
          findViewById(R.id.die5)
        )
Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
0

Length is constant which is used to find out the array storing capacity not the number of elements in the array.

Number of elements in the array is called size.

If you use array with 5 elements, your last element must have index 4. Because all arrays start from index 0.

Difference between size and lenght

Roman Shubenko
  • 322
  • 2
  • 13