2

After Convert Java File to Kotlin File, gives me two errors in same line: In code below show variables and function with error

     lateinit var ncArr: Array<ImageButton>
     lateinit var xBitmap: Bitmap
     lateinit var oBitmap: Bitmap
     lateinit var intArr: IntArray
     lateinit var btnStartGame: Button
     lateinit var btnMenu: Button
     var stop: Boolean = false
     var gameMode: Int = 0
     var umove: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onStart()
    setContentView(R.layout.activity_game)

    ncArr = arrayOfNulls(9)

    ncArr[0] = findViewById(R.id.nc0) as ImageButton
    ncArr[1] = findViewById(R.id.nc1) as ImageButton
    ncArr[2] = findViewById(R.id.nc2) as ImageButton
    ncArr[3] = findViewById(R.id.nc3) as ImageButton
    ncArr[4] = findViewById(R.id.nc4) as ImageButton
    ncArr[5] = findViewById(R.id.nc5) as ImageButton
    ncArr[6] = findViewById(R.id.nc6) as ImageButton
    ncArr[7] = findViewById(R.id.nc7) as ImageButton
    ncArr[8] = findViewById(R.id.nc8) as ImageButton

    ncArr[0].setOnClickListener(this)
    ncArr[1].setOnClickListener(this)
    ncArr[2].setOnClickListener(this)
    ncArr[3].setOnClickListener(this)
    ncArr[4].setOnClickListener(this)
    ncArr[5].setOnClickListener(this)
    ncArr[6].setOnClickListener(this)
    ncArr[7].setOnClickListener(this)
    ncArr[8].setOnClickListener(this)

    xBitmap = BitmapFactory.decodeResource(resources, R.drawable.x)
    oBitmap = BitmapFactory.decodeResource(resources, R.drawable.o)

    intArr = IntArray(9)

    for (i in 0..8) {
        intArr[i] = 0
    }

    btnMenu = findViewById(R.id.btnMenu) as Button
    btnStartGame = findViewById(R.id.btnStartGame) as Button
    btnMenu.setOnClickListener(this)
    btnStartGame.setOnClickListener(this)

    stop = false

    gameMode = intent.getIntExtra("game_mode", 1)
    umove = 1
}

in line ncArr = arrayOfNulls(9) There are errors: enter image description here

I trying to fix this problem but i don't have more ideas...

Any sollution?

thanks in advance

2 Answers 2

2
ncArr = arrayOfNulls(9)

The type of ncArr will be Array<ImageButton?>

ncArr[0] = findViewById(R.id.nc0) as ImageButton

You are using unsafe cast operator as, because the cast operator throws an exception if the cast is not possible. Thus, we call it unsafe. What happens if

findViewById(R.id.nc0) as ImageButton

return null, then null cannot assign to ImageButton, it will throw an exception and make your app crash.

To avoid the above error you can use

ncArr[0] = findViewById(R.id.nc0) as ImageButton?

or using safe cast operator as?

ncArr[0] = findViewById(R.id.nc0) as? ImageButton

Remember apply for ncArr[0] to ncArr[8].

Update: Based on your request, you can use this solution:

lateinit var ncArr: Array<ImageButton>
    lateinit var xBitmap: Bitmap
    lateinit var oBitmap: Bitmap
    lateinit var intArr: IntArray
    lateinit var btnStartGame: Button
    lateinit var btnMenu: Button
    var stop: Boolean = false
    var gameMode: Int = 0
    var umove: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onStart()
        setContentView(R.layout.activity_game)

        ncArr = arrayOf(
                findViewById<ImageButton>(R.id.nc0).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc1).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc2).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc3).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc4).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc5).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc6).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc7).apply{setOnClickListener(this@GameActivity)},
                findViewById<ImageButton>(R.id.nc8).apply{setOnClickListener(this@GameActivity)}

        )

        xBitmap = BitmapFactory.decodeResource(resources, R.drawable.x)
        oBitmap = BitmapFactory.decodeResource(resources, R.drawable.o)

        // Your code here
        ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

is correct this way: ncArr[0] = findViewById<ImageButton?>(R.id.nc0)
I see your problem, change lateinit var ncArr: Array<ImageButton> to lateinit var ncArr: Array<ImageButton?>
0

You trying to put Nullable array into NonNull. You may declare intArr as Array<ImageButton?> and everything will work.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.