0

I am trying to convert a return new array in Java to Kotlin. I have tried using the docs but doesn't seem to work.

Below is the java code

public ScoreController[] newArray(int size) {
        return new ScoreController[size];
    }

Below is Kotlin code that I trying to do

override fun newArray(size: Int): Array<GenderController> {
            return arrayOfNulls(size)
        }

The kotlin code isn't happy :(

1 Answer 1

3

You're explicitly returning an Array that contains null values, you have to adjust your return type to allow for that:

override fun newArray(size: Int): Array<GenderController?> {
    return arrayOfNulls(size)
}

This can be seen from the signature of the arrayOfNulls function, it returns an Array<T?>:

fun <reified T> arrayOfNulls(size: Int): Array<T?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.