-3

My app has a tab bar with three fragments. During the onCreate Method I call a function called initPlantList. This function initializes my RecyclerListAdapter.

private void initPlantList(){
    RecyclerView rV = findViewById(R.id.plantsRecycler);
    PlantListAdapter pLA = new PlantListAdapter(this);
    Log.d("PLA", pLA.hasContext());
    rV.setAdapter(pLA);
    rV.setLayoutManager(new LinearLayoutManager(this));
}

I get this error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference when I attempt to call rV.setAdapter(pLA); Here is my Adapter Class

public class PlantListAdapter extends 
RecyclerView.Adapter<PlantListAdapter.ViewHolder>{

    private static final String TAG = "PlantListAdapter";

    private Context mContext;

    public PlantListAdapter(Context c){
        this.mContext = c;
    }

    public String hasContext(){
        if(this.mContext != null){
            return mContext.getPackageResourcePath();
        }
        return "false";
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layoutplantlist, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Log.d(TAG, "onBindViewHolder: called.");

    holder.image.setImageDrawable(MyApplication.myPlantList.get(position).getImage());

holder.plantName.setText(MyApplication.myPlantList.get(position).getName());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        CircleImageView image;
        TextView plantName;
        RelativeLayout plantListItem;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.plantImage);
            plantName = itemView.findViewById(R.id.plantName);
            plantListItem = itemView.findViewById(R.id.plantListItem);
        }
    }
}

Any thoughts on why it is reffering to pLA as a null object reference?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Gabe Spound
  • 146
  • 2
  • 14

1 Answers1

1

It is not your adapter that is null. Its is your Recyclerview. You are using a fragment therefore you can’t use (findViewById). You have to find it using the view you inflated. Try this:

private void initPlantList(View v){
RecyclerView rV = v.findViewById(R.id.plantsRecycler);
PlantListAdapter pLA = new PlantListAdapter(this);
Log.d("PLA", pLA.hasContext());
rV.setAdapter(pLA);
rV.setLayoutManager(new LinearLayoutManager(this));

}

And when you call that method in your fragment onCrrateView() just pass the view as a parameter. Hope it helps you

timtimer
  • 71
  • 2
  • 9
  • This has been very helpful. The problem is, I am calling `initPlantList()` in `Fragment.getItem(int position)`. I only want this function called when the leftMost tab is clicked, so I have it inside a `switch (position)` in `case 0:`. There is no view here for me to pass in. Is there any way to move `initPlantList()` to `onCreateView()` and retain this functionality? EDIT: I tried moving it into `onCreateView`, but I am unable to because the `PlantListAdapter` constructor requires context to be created, which can't be made `static` (`onCreateView` is `static`) – Gabe Spound Apr 27 '18 at 18:40
  • You have three fragments. That means you have three layouts Each fragment has its own view in which it inflates. Therefore, put all the codes related to that fragment inside that fragment. For better help, can u also show you fragment class? Also the activity in which you populate those fragments. Then i’ll edit my answer no problem – timtimer Apr 27 '18 at 18:48
  • I understand, I moved the `initPlantList` function to it's own fragment class instead of just leaving it in `mainactivity` and everything works great. Thank you very much! – Gabe Spound Apr 27 '18 at 18:55