Search code examples
javaandroidadapternullreferenceexception

Get a null reference while inflating a layout fromContext in myAdapter


Application keeps crashing and I keep getting the error below:

java.lang.NullPointerException: Attempt to invoke virtual method java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
    at android.view.LayoutInflater.from(LayoutInflater.java:229)
    at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:37)
    at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:19)

Here's my code:

public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.ImageViewHolder> {

    private Context mContext;
    private List<Popular> mPopular;

    public PopularAdapter(Context context, List<Popular> populars){
        this.mContext = context;
        mPopular = populars;
    }

    public PopularAdapter(List<Popular> mPopular) {
        this.mPopular = mPopular;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.popular_items,viewGroup, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
        Popular popularCur=mPopular.get(i);
        imageViewHolder.prod_name.setText(popularCur.getProduct_title());
        imageViewHolder.prod_price.setText(popularCur.getProduct_price());
        Picasso.get()
                .load(popularCur.getProduct_image())
                .placeholder(R.drawable.img_placeholder)
                .fit()
                .centerCrop()
                .into( imageViewHolder.prod_img);
    }

    @Override
    public int getItemCount() {
        return mPopular.size();
    }

    class ImageViewHolder extends RecyclerView.ViewHolder {
         TextView prod_name, prod_price;
         ImageView prod_img;

         ImageViewHolder(@NonNull View itemview){
            super(itemview);
            prod_name = itemview.findViewById(R.id.prodName);
            prod_price = itemview.findViewById(R.id.prodPrice);
            prod_img = itemview.findViewById(R.id.prodImage);
        }
    }
}

Please, I really need help understanding what I am doing wrong and a beginner in android. It seems to me that the Context has been nullified at some point in the code. I don't seem to know where and how I tried working with fragments has been a little confusing for me.


Solution

  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
            at android.view.LayoutInflater.from(LayoutInflater.java:229)
            at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:37)
            at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:19)
    

    the above error is telling you that you're trying to use a null Context to inflate your Adapter item layout.

    It's because you have two constructor: one with context and the other is not.

    // Here you're passing the context to the adapter
    public PopularAdapter(Context context, List<Popular> populars){
        ...
    }
    
    // No context here.
    public PopularAdapter(List<Popular> mPopular) {
        ...
    }
    

    and it seems that you call the no context constructor. Hence the context is null.

    You can avoid this problem by using the Adapter ViewGroup internally instead of passing the context from the caller. You can update your Adapter to something like this:

    public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.ImageViewHolder> {
    
        private List<Popular> mPopular;
    
        // Use only one constructor.
        public PopularAdapter(List<Popular> mPopular) {
            this.mPopular = mPopular;
        }
    
    
        @NonNull
        @Override
        public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    
          // Use the viewGroup i.e parent of the Adapter.
          Context context = viewGroup.getContext();
          LayoutInflater inflater = LayoutInflater.from(context);
    
          // Inflate the layout
          View v = inflater.inflate(R.layout.popular_items, viewGroup, false);
          return new ImageViewHolder(v);
        }
    
        ...
    
    }