How can I solve nullity error on kotlin while using EditText?

Viewed 39

Complete beginner, trying this on kotlin. I am trying to capture a photo and save it and then load it on room database. I also need tag the photo and load it according to that. But there is an error like "Caused by: java.lang.NullPointerException: findViewById(R.id.metin) must not be null". How I can solve it? Thanks in advance.

class MainActivity : AppCompatActivity() {

    private val cameraRequest = 1888
    lateinit var imageView: ImageView
    lateinit var textbox : EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var dao_object = ImageDatabase.getInstance(application).MyDao()

        textbox = findViewById(R.id.metin)



        setContentView(R.layout.activity_main)
        if (ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_DENIED
        )
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA),
                cameraRequest
            )

        imageView = findViewById(R.id.imageView)
       // var textbox: EditText = findViewById(R.id.text)
        val photoButton: Button = findViewById(R.id.capture_button)

        photoButton.setOnClickListener {
            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(cameraIntent, cameraRequest)
        }


        val saveButton: Button= findViewById(R.id.save_button)
        saveButton.setOnClickListener{
            val takenPhoto: ByteArray = DbBitmapUtility.getBytes(imageView.drawingCache)
            dao_object.insertImage(MyDatabase(takenPhoto, textbox.text.toString()))
        }

        val loadButton: Button= findViewById(R.id.load_button)
        loadButton.setOnClickListener {
            var displayPhoto: ByteArray = dao_object.getImageByTag(textbox.text.toString())
            imageView.setImageBitmap(DbBitmapUtility.getImage(displayPhoto))
        }


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == cameraRequest) {
            val photo: Bitmap = data?.extras?.get("data") as Bitmap
            imageView.setImageBitmap(photo)
        }
    }

    object DbBitmapUtility {
        // convert from bitmap to byte array
        fun getBytes(bitmap: Bitmap): ByteArray {
            val stream = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream)
            return stream.toByteArray()
        }
        // convert from byte array to bitmap
        fun getImage(image: ByteArray): Bitmap {
            return BitmapFactory.decodeByteArray(image, 0, image.size)
        }
    }
}
2 Answers

Change the arrangement of the code. Rather than doing the following

textbox = findViewById(R.id.metin)
setContentView(R.layout.activity_main)

do:

setContentView(R.layout.activity_main)
textbox = findViewById(R.id.metin)

setContentView must first be called before you can attempt any manipulation on the activity's view.

You can't use findViewById before you've called setContentView(). There is no View to search in for your view yet at that point.

Related