-1

I have a problem in my app where I need to display an image with recyclerview but I'm getting a null object.

class AllSubscribersAdapter(val context: Context, private val subsribedChannels: SubscribedChannels): RecyclerView.Adapter<AllSubscribersAdapter.ViewHolder>() {

    private var channels: List<Channels> = listOf()
    init {
        channels = subsribedChannels.channels
    }

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.subscribers_item, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return channels.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.tvChannelName.text = channels[position].name

        Glide.with(holder.channel_img)
            .load(channels[position].avatar)
            .transform(CircleCrop())
            .into(holder.channel_img)

        /*Picasso.get()
            .load(channels[position].avatar)
            .fit()
            .into(holder.channel_img)*/

    }

    fun setChannelsList(channels: List<Channels>){
        this.channels = channels
        notifyDataSetChanged()
    }

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

        var tvChannelName: TextView = itemView!!.findViewById(R.id.channel_name)
        var channel_img: ImageView = itemView!!.findViewById(R.id.channel_image)
    }

}

channel_name is working but channel_image is null, any help is appreciated. Here is my model class.

class SubscribedChannels (
    var channels: List<Channels>
): Serializable

data class Channels(
    var id: String,
    var name: String,
    var slug: String,
    var avatar: String,
    var created_by: String,
    var subscribers: String
): Serializable

I don't know if there is a problem here.

Erald Developer
  • 359
  • 1
  • 6
  • 16
  • 1
    Please post your retrofit response and post the logcat – Ashish Dec 20 '19 at 13:10
  • post what is the type and value of channels[position].avatar if the value is null, then your problem is not with the adapter, but with retrofit. – Angel Koh Dec 20 '19 at 13:12
  • 1
    share R.layout.subscribers_item xml code if you are getting channel_image (ImageView) null from findViewById – Hamza Khan Dec 20 '19 at 13:14
  • Does this answer your question? [Null Point exception](https://stackoverflow.com/questions/24340807/null-point-exception) – Erfan Dec 21 '19 at 10:40
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Dec 22 '19 at 02:50

2 Answers2

0

First thing you cannot initialize variable in ViewHolder class. You should do it in constructor just like this.

 var tvChannelName: TextView?=null
 var channel_img: ImageView?=null
 init{
       tvChannelName = itemView!!.findViewById(R.id.channel_name)
      channel_img = itemView!!.findViewById(R.id.channel_image)
     }

And if you are getting empty string in your retrofit response then you have check string then you have to call glide.

Rahul sharma
  • 1,492
  • 12
  • 26
0

At the bottom holder.tvChannelName.text you can log chanels. Avatar if is not empty you can check the value have the valid image url

adi purnama
  • 156
  • 2
  • 6