Home > Net >  java.lang.NullPointerException: uriString , when trying to convert image from room DB from bitmab to
java.lang.NullPointerException: uriString , when trying to convert image from room DB from bitmab to

Time:01-26

I'm trying to convert image from database from bitmap to URI on button clicked, then, pass image with data for selected row to (EditCartFragment.kt), but i have this error (java.lang.NullPointerException: uriString).

EditCartFragment.kt

override fun passData(position: Int, title: String, image: Bitmap, price: Double, priceI: Double, num: Int, des: String) {
        try {
            val bundle = Bundle()
            bundle.putInt("edit_pos", position)
            bundle.putString("edit_name", title)
            val img = getImageUri(requireContext(), image).toString()
            bundle.putString("edit_image", img)
            bundle.putDouble("edit_price", price)
            bundle.putDouble("edit_priceI", priceI)
            bundle.putInt("edit_num", num)
            bundle.putString("edit_des", des)

            val transaction = this.parentFragmentManager.beginTransaction()
            val editSingleItemFragment = EditSingleItemFragment()
            editSingleItemFragment.arguments = bundle

            transaction.replace(R.id.fragment_container, editSingleItemFragment)
            transaction.addToBackStack("editSI_fragment").setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            transaction.commit()
        } catch (e: NullPointerException){
            Toast.makeText(context, "Fuck", Toast.LENGTH_SHORT).show()
        }
    }
    //Converting bitmap image to URL
    private fun getImageUri(inContext: Context, inImage: Bitmap): Uri? {
        val bytes = ByteArrayOutputStream()
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
        val path = Images.Media.insertImage(inContext.contentResolver, inImage, "Title", null)
        return Uri.parse(path)
    }

CartEditAdapter.kt

inner class MyViewHolder constructor(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener{

        init {
            //Setting decrease items
            itemView.editMyItem.setOnClickListener(this)
        }

        override fun onClick(v: View?) {
            val position = adapterPosition
            val image = cartItems[adapterPosition].cartImage
            val name =  cartItems[adapterPosition].cartTitle
            val price = cartItems[adapterPosition].cartPriceL
            val priceI = cartItems[adapterPosition].cartPriceI
            val num = cartItems[adapterPosition].cartNum
            val des = cartItems[adapterPosition].cartDes
            if (position != RecyclerView.NO_POSITION) {
                listener.passData(position,name!!, image!!, price!!, priceI!!, num!!, des!!)
            }
        }

        val cartedImg: ShapeableImageView = itemView.myCartedImg
        val cartedTitle: TextView = itemView.myCartedTitle
        val cartedItemNum: TextView = itemView.myCartItemNum
        val cartedLPriceValue: TextView = itemView.localPriceCart
        val cartedIPriceValue: TextView = itemView.localPriceCartA
        val cartedDes: TextView = itemView.myCartedDes
    }

CodePudding user response:

The error you are getting, "java.lang.NullPointerException: uriString," is occurring because the getImageUri method is returning a null value. This could be because the inImage parameter passed to the method is null.

Here is one possible solution:

You can check if the image passed is null or not before calling the getImageUri method by adding a null check like this:

if(image!=null){
val img = getImageUri(requireContext(), image).toString()
bundle.putString("edit_image", img)
}

Another thing you could try is to check if the Uri.parse(path) is not null before using it.

val path = Images.Media.insertImage(inContext.contentResolver, inImage, "Title", null)
val uri = Uri.parse(path)
return if(uri!=null) uri else null

If the issue persists, please check if the Bitmap image is not null when you are getting it from the database and make sure the image is not corrupted. Also, make sure you have the correct permissions in your manifest file.

  • Related