0

the fun of superclass like this:

fun onTrigger(params: P) {
    if (!funA(params)) {
        return
    }
    val request = funB(params)
}

funA and funB is abstract fun , implement in subclass like this(the generic P is Int):

fun funA(params:Int): Boolean {
    if (params < 0) {
        return false
    }
}

fun funB(params:Int) {
    val item = dataList[params]
}

accidental appearance crash:
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1

the params can be -1? why?

Joe Blake
  • 1
  • 2
  • Does this answer your question? [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) – MyStackRunnethOver Nov 16 '21 at 13:41
  • The code you showed does not even compile. Please show a [mcve]. – Sweeper Nov 16 '21 at 13:44
  • Hi and welcome to Stack Overflow! :) In general, please make sure to always do a quick search to see if your problem already has an answer (I think yours does, since ArrayIndexOOB exceptions are extremely common :)). If it does not, then ask away! But be sure to include a *Minimal, Reproducible Example* [see link](https://stackoverflow.com/help/minimal-reproducible-example) (in this case, you don't actually need to show us most of your code in order to show us what's going wrong). Often, building a minimal reproducible example can help you find that there *is* an existing answer :) – MyStackRunnethOver Nov 16 '21 at 13:44
  • ``funA`` is checking the value of ``position``, not ``params``. Whatever the real code looks like, use the debugger to step through and see what's happening, that will help you work things out – cactustictacs Nov 16 '21 at 13:51
  • the real code has too much unrelate code, so i simplify it – Joe Blake Nov 16 '21 at 14:21
  • Show the real code, because as is it does not even compile and nothing guarantees us that you didn't miss something important while simplifying it. Thanks! – João Dias Nov 16 '21 at 15:16
  • Please provide enough code so others can better understand or reproduce the problem. – Community Nov 16 '21 at 22:37

0 Answers0