0

I have received a pre-launch report saying there is a divideByZero runtime error when their test deselects both of two checkboxes ("Fractions to Decimals" and "Decimals to Fractions". This makes sense, except that I keep the user from doing so with the following code. How come their testing bots are able to get to the else part of the when function? What is the appropriate way to make this more bullet proof? Do I need to be coding with a different logic in mind, namely one that those bots can do?

override fun onClick(view: View?) {
    when(view?.id) {
        R.id.btn_to_main -> {
            val noFracSelected: Boolean = !(Constants.ASK_8 || Constants.ASK_16 || Constants.ASK_32 || Constants.ASK_64) //true if frac type is selected
            val noQuestionTypeSelected: Boolean = !(Constants.ASK_DtoF || Constants.ASK_FtoD)
            when {
                noFracSelected -> Toast.makeText(this, "Select at least one type of fraction", Toast.LENGTH_LONG).show()
                noQuestionTypeSelected-> Toast.makeText(this, "Select at least one type of question", Toast.LENGTH_LONG).show()
                else -> {
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                }
            }
        }
    }
}

Screenshot of the app

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • You shouldn't be storing the user selectable booleans in a "Constants" object. Probably better to store them in a viewmodel, or read the checkbox values directly when the button is clicked. In regards to your question, I don't see a logical error in this bit of code, but you could make it more bulletproof by not allowing all checkboxes to be deselected simultaneously. When the last box is being deselected by the user, prevent that and instead show an error. – Rish K Apr 03 '22 at 06:21
  • Thanks Rishabh, I appreciate the comment for not saving it in an object called constants. I need those values in other sheets, and was following a tutorial and just dumped it into the constants object already created. With regards to the actual issue, I left this code alone and wrapped the area where it divides by number of questions on another sheet with a try/ catch method, and return 0 if it is divide by zero. This allowed the app to be published. – ohSnapFit Apr 07 '22 at 14:50

0 Answers0