Home > Net >  I have an FATAL EXCEPTION on my setImageResource and I don't get why
I have an FATAL EXCEPTION on my setImageResource and I don't get why

Time:06-07

I'm building a boardgame manager app using kotlin for a school project, and I'm new at using Kotlin and Android Studio. I've decided to adapt an online tutorial to my needs so I can build my own app. But this error appears when I launch the app and I don't understand why :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

I've tried to launch it in debug mode so I may find the problem but I don't understand why it is mentionned that it can be null.

I use a realtime firebase database, and here is the code of my BgAdapter, where the FATAL EXCEPTION appears :

class BgAdapter(
val context: MainActivity,
private val bgList: List<BgModel>,
private val layoutId: Int) : RecyclerView.Adapter<BgAdapter.ViewHolder>() {

//Boîte pour ranger tout les composants à contrôler
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val bgImage = view.findViewById<ImageView>(R.id.image_item)
    val bgName: TextView? = view.findViewById(R.id.name_item)
    val bgEditor: TextView? = view.findViewById(R.id.editor_item)
    val bgFavIcon = view.findViewById<ImageView>(R.id.add_light_icon)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater
        .from(parent.context)
        .inflate(layoutId, parent, false)

    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    //Récupérer les informations des bgs
    val currentBg = bgList[position]

    //Récupérer le repository
    val repo = BgRepository()

    //Utiliser glide pour récupérer l'image à partir de son lien -> composant
    Glide.with(context).load(Uri.parse(currentBg.image_url)).into(holder.bgImage)

    //Mettre à jour le nom du jeu
    holder.bgName?.text = currentBg.name

    //Mettre à jour le nom de l'éditeur du jeu
    holder.bgEditor?.text = currentBg.editor

    //Vérifier si le jeu a été ajouté ou non
    if (currentBg.faved) {
        holder.bgFavIcon.setImageResource(R.drawable.ic_check)
    } else {
        holder.bgFavIcon.setImageResource(R.drawable.ic_add_light)
    }

    //Rajouter une interaction sur icône checkbox
    holder.bgFavIcon.setOnClickListener {
        //Inverse si le bouton est en mode "ajouté" ou non
        currentBg.faved = !currentBg.faved
        //Mettre à jour l'objet jeu
        repo.updateBg(currentBg)
    }

    //Interaction lors du clic sur un jeu
    holder.itemView.setOnClickListener {
        //Afficher la popup
        BgInfoPopup(this, currentBg).show()
    }
}

override fun getItemCount(): Int = bgList.size

}

The FATAL EXCEPTION is targetting specificaly this part :

        if (currentBg.faved) {
        holder.bgFavIcon.setImageResource(R.drawable.ic_check)
    } else {
        holder.bgFavIcon.setImageResource(R.drawable.ic_add_light)
    }

If you have any idea for an unexperienced student who really needs help, feel free to answer ! :)

Thank you by advance

CodePudding user response:

I think you might need to check the layoutId you passed into the BgAdapter to see if R.id.add_light_icon is included

CodePudding user response:

You have to check the R.id.add_light_icon in your layout. If there is nothing wrong in your code may be you are inflating wrong layout and getting null view may be there is no view present in your layout or passing wrong id.

  • Related