0

Here's my Question: I'm in the middle of a tutorial on how to create mobile applications and I'm encountering some real difficulties. Everything was working fine until I added the following line of code to allow me to access an online database:

GlideApp.with(context).load(Uri.parse(currentPlant.imageUrl)).into(holder.plantImage)

To try to solve the problem, I went to the Run and got the messages below. I was able to identify the error (java.lang.NullPointerException: Argument must not be null) but I don't know how to solve it. I absolutely need the help of others, I've been at it for hours

 2021-02-17 03:09:01.020 30727-30727/de.laurette.naturecollection E/AndroidRuntime: FATAL EXCEPTION: main
    Process: de.laurette.naturecollection, PID: 30727
    java.lang.NullPointerException: Argument must not be null
        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:23)
        at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:768)
        at de.laurette.naturecollection.adapter.PlantAdapter.onBindViewHolder(PlantAdapter.kt:49)
        at de.laurette.naturecollection.adapter.PlantAdapter.onBindViewHolder(PlantAdapter.kt:25)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
        at android.view.View.layout(View.java:22844)
        at android.view.ViewGroup.layout(ViewGroup.java:6389)
2021-02-17 03:09:01.021 30727-30727/de.laurette.naturecollection E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-02-17 03:09:01.164 30727-30727/de.laurette.naturecollection I/Process: Sending signal. PID: 30727 SIG: 9

The PlantAdapter class contains the method or the line of code that generates the error:

    //cette classe sera donnee a notre recyclerview pour pouvoir adapter pour chaque plante son equivalent en image
class PlantAdapter(
    private val context : MainActivity,
    private val plantList : List<PlantModel> ,
    private val layoutId: Int
    ) : RecyclerView.Adapter<PlantAdapter.ViewHolder>() {
            // elle permettra de porter la vue , une sorte de boite pour ranger touts les composants a controller
            class ViewHolder(view : View) : RecyclerView.ViewHolder(view) {
                val plantImage = view.findViewById<ImageView>(R.id.image_item)
                //image de la plante
            }
    // nous permet dinjecter un composant
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =  LayoutInflater.from(parent.context).inflate(layoutId, parent,false)

        return ViewHolder(view)
    }

    // permettra de mettre a jour chaque modele avec la plante en question
    override fun onBindViewHolder( holder: ViewHolder, position: Int) {
        // recuperer les informations de la plante
        val currentPlant = plantList[position]

        // utiliser glide pour recuperer l'image á partir de son lien et venir laajouter a notre composant
        //GlideApp.with(context).load(Uri.parse(currentPlant.imageUrl)).into(holder.plantImage)
        GlideApp.with(context).load(Uri.parse(currentPlant.imageUrl)).into(holder.plantImage)

    }

    // permettra de renvoyer combien d'items on affichera dynamiquement
    override fun getItemCount(): Int = plantList.size

the next class represents the class where the image and its characteristics are added, including the imageUrl. The images had to be imported from the Pixabay online database. I have much more the impression that the link to the image doesn't work properly, I'm not sure. Could you please tell me concretely what to do to solve my problem ? Unfortunately I don't have a solid base in Programming to solve this problem. I am working on android and the phone crashes after being launched and sends back the message "keeps Stopping". I use Android studio and the gradle version is Gradle 4.1.2.

package de.laurette.naturecollection.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import de.laurette.naturecollection.MainActivity
import de.laurette.naturecollection.PlantModel
import de.laurette.naturecollection.R
import de.laurette.naturecollection.adapter.PlantAdapter
import de.laurette.naturecollection.adapter.PlantItemDecoration


class HomeFragment (
        private val context : MainActivity
        ) : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater?.inflate(R.layout.fragment_home, container , false )

        //creer une liste qui va stocker ces  plantes
        val plantList = arrayListOf<PlantModel>()

        // enregistrer une premiere plante dans notre liste (pissenlit)
        plantList.add(PlantModel(
            "pissenlit",
            "jaune soleil",
           "https://cdn.pixabay.com/photo/2014/05/01/17/51/dandelion-335662_960_720.jpg",
            false
        ))

        // enregistrer une deuxieme plante dans notre liste (Rose)
        plantList.add(PlantModel(
         "Rose",
         "ca pique un peu",
            "https://cdn.pixabay.com/photo/2018/05/16/22/27/rose-3407234_960_720.jpg",
         false
    ))

        // enregistrer une troisieme plante dans notre liste (castus)
        plantList.add(PlantModel(
         "castus",
         "ca pique beaucoup",
         "https://cdn.pixabay.com/photo/2016/07/06/20/47/prickly-pear-1501307_960_720.jpg",
         false
        ))

        // enregistrer une quatrieme plante dans notre liste (Tulipe)
        plantList.add(PlantModel(
            "Tulipe",
            "c'est beau'",
            "https://cdn.pixabay.com/photo/2017/03/13/21/19/tulip-2141216__340.jpg",
            false
        ))

        // recuperer le recyclerview
        val horizontalRecyclerView = view.findViewById<RecyclerView>(R.id.horizontal_recycler_view)
        horizontalRecyclerView.adapter = PlantAdapter(context, plantList, R.layout.item_horizontal_plant)

        // recuperer le second recyclerview
        val verticalRecyclerView = view.findViewById<RecyclerView>(R.id.vertical_recycler_view)
        verticalRecyclerView.adapter = PlantAdapter(context, plantList, R.layout.item_vertical_plant)
        verticalRecyclerView.addItemDecoration(PlantItemDecoration())

        return view

   }
}

GlideModule

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey

@GlideModule
class AppNameGlideModule : AppGlideModule() {
    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)
        builder.apply { RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).signature(ObjectKey(System.currentTimeMillis().toShort()))}
    }
}

0 Answers0