1

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 Answers2

1

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.

raiyan22
  • 1,043
  • 10
  • 20
Meggrain
  • 178
  • 10
  • Thank you so much! With help I handled this problem. But after that, there is an error "java.lang.NullPointerException: imageView.drawingCache must not be null", any idea what must be the problem? Thanks in advance. – Josh Conner Jan 06 '22 at 17:44
  • @JoshConner it's a different problem then the one you posted. You should ask a different question. – saiful103a Jan 06 '22 at 17:45
  • this link will be useful https://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null – Meggrain Jan 06 '22 at 17:51
0

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

Tenfour04
  • 83,111
  • 11
  • 94
  • 154