Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

property getter or setter expected

Tags:

android

kotlin

I want to use a bitmap var in a class. It makes 'property getter or setter expected' error. What is the problem? The error shows around 'bmp? : Bitmap = null'. How can I solve the problem?

And I don't understand why I must use getter or setter for private properties in a class.

class MyView(context: Context?) : View(context) {
    private var bmp? : Bitmap = null

    init {
        bmp = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawColor(Color.BLUE)
        canvas?.drawBitmap(bmp,10f,10f, null)
    }
}
like image 472
Kim Sangdeuk Avatar asked Dec 13 '22 12:12

Kim Sangdeuk


2 Answers

Issue is that you're going to create Nullable object using safe call operator, but your syntax is wrong. Inspite of placing ? at variable, you'll need to put it at Reference type.

Check correct syntax :

private var bmp : Bitmap? = null

And then you can access this variable with safe call operator like below :

bmp?.someMethodCall() // This line will never throw you null pointer exception because of ? (Safe call operator)

Check out more here.

like image 89
Jeel Vankhede Avatar answered Dec 17 '22 23:12

Jeel Vankhede


Please try below line

lateinit var bmp : Bitmap
like image 27
Mr.vicky patel Avatar answered Dec 17 '22 23:12

Mr.vicky patel