Jo.........
Jo.........

Reputation: 330

Getting Target Null Error message in Kotlin using Picasso

I am trying to display image using Json API but I am getting Target not null error message. I have three classes one main, adapter and model class. I have already added picasso library and the name is working fine without issue and image is an issue. Any help is appreciated.

Main Class:

var Test:String=catObj.getString("Test")
category.Test=Test
Picasso.get().load(category.Test).into(TestImage)---TestImage is my ImageView ID.

Adapter class:

class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {

    fun bindItem(Test:TestModel)
    {
        var name:TextView=itemView.findViewById<TextView>(R.id.CatName)
        var picture:TextView=itemView.findViewById<TextView>(R.id.CatImage)
        name.text=Test.name
        picture.text=Test.location

    }
}

Error Log:

  java.lang.IllegalArgumentException: Target must not be null.

Upvotes: 0

Views: 2977

Answers (2)

unzila
unzila

Reputation: 300

Try this in kotlin its working

 Picasso.get().load(category.picture).into(object : com.squareup.picasso.Target {
            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
                TODO("not implemented") 
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                // loaded bitmap is here (bitmap)

                holder.binding.imageView.setImageBitmap(bitmap)
            }

            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
            
        })
    }

Upvotes: 8

Jo.........
Jo.........

Reputation: 330

Thanks Every one for their response. I solved it via below changes in my code.

Main Class:

var Test:String=catObj.getString("Test")
category.Test=Test

Adapter Class:

class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {

    fun bindItem(Test:TestModel)
    {
        var name:TextView=itemView.findViewById<TextView>(R.id.CatName)
        var picture:ImageView=itemView.findViewById<ImageView>(R.id.CatImage)
        name.text=Test.name
        Picasso.get().load(category.picture).into(location)
    }
}

Upvotes: 1

Related Questions