title: How to set RecyclerView OnClickListener in Fragments?
body: <p>I'm trying to start a new <code>Fragment</code> that shows the details of the item that was clicked on in the <code>RecyclerView</code>. I've tried this in Activities and it worked perfectly but I'm trying to convert it to use on Fragments. Every time I get an <em>'null object reference'</em>. </p>
  
  <p><strong>Adapter</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package com.umbrella.fragmenttest;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.navigation.NavController;
  import androidx.navigation.Navigation;
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.List;
  
  public class BorrowedAdapter extends RecyclerView.Adapter&lt;BorrowedAdapter.BorrowedViewHolder&gt; {
  
      private Context mCtx;
      private List&lt;Borrowed&gt; borrowedList;
  
      public BorrowedAdapter(Context mCtx, List&lt;Borrowed&gt; borrowedList) {
          this.mCtx = mCtx;
          this.borrowedList = borrowedList;
      }
  
      @Override
      public BorrowedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(mCtx).inflate(R.layout.list_borrowed, parent, false);
          return new BorrowedViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(BorrowedViewHolder holder, int position) {
          Borrowed b = borrowedList.get(position);
  
          holder.textViewOwner.setText(b.getOwner());
          holder.textViewDesc.setText(b.getDesc());
          holder.textViewAmount.setText(String.valueOf(b.getAmount()));
      }
  
      @Override
      public int getItemCount() {
          return borrowedList.size();
      }
  
      class BorrowedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
          TextView textViewOwner, textViewDesc, textViewAmount;
  
          public BorrowedViewHolder(View itemView) {
              super(itemView);
  
              textViewOwner = itemView.findViewById(R.id.b_owner);
              textViewDesc = itemView.findViewById(R.id.b_description);
              textViewAmount = itemView.findViewById(R.id.b_amount);
  
              itemView.setOnClickListener(this);
          }
  
          @Override
          public void onClick(View view) {
              Borrowed borrowed = borrowedList.get(getAdapterPosition());
  
              AppCompatActivity activity = (AppCompatActivity) view.getContext();
              NavController navController = Navigation.findNavController(activity, R.id.hostfrag);
              navController.navigate(R.id.action_displayBorrowed2_to_updateBorrowedClass);
          }
      }
  }
  </code></pre>
  
  <p><strong>Fragment class with the details</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package com.umbrella.fragmenttest;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.fragment.app.Fragment;
  
  
  public class UpdateBorrowedClass extends Fragment {
      private EditText editTextOwner, editTextDesc, editTextAmount;
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.new_borrowed, container, false);
  
          editTextOwner = view.findViewById(R.id.u_owner);
          editTextDesc = view.findViewById(R.id.u_description);
          editTextAmount = view.findViewById(R.id.u_amount);
  
          Bundle bundle = getArguments();
          final Borrowed borrowed = (Borrowed) bundle.getSerializable("borrowed");
  
          loadBorrowed(borrowed);
  
          view.findViewById(R.id.button_update).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  updateBorrowed(borrowed);
              }
          });
  
          view.findViewById(R.id.button_delete).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                  builder.setTitle("Are you sure?");
                  builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          deleteBorrowed(borrowed);
                      }
                  });
                  builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
  
                      }
                  });
  
                  AlertDialog ad = builder.create();
                  ad.show();
              }
          });
  
         return view;
      }
  
      private void loadBorrowed(Borrowed borrowed) {
          editTextOwner.setText(borrowed.getOwner());
          editTextDesc.setText(borrowed.getDesc());
          editTextAmount.setText(String.valueOf(borrowed.getAmount()));
      }
  
      private void updateBorrowed(final Borrowed borrowed) {
          final String sOwner = editTextOwner.getText().toString().trim();
          final String sDesc = editTextDesc.getText().toString().trim();
          final Double dAmount = Double.parseDouble(editTextAmount.getText().toString().trim());
  
          if (sOwner.isEmpty()) {
              editTextOwner.setError("Owner required");
              editTextOwner.requestFocus();
              return;
          }
  
          if (sDesc.isEmpty()) {
              editTextDesc.setError("Description required");
              editTextDesc.requestFocus();
              return;
          }
  
          if (dAmount.toString().isEmpty()) {
              editTextAmount.setError("Amount required");
              editTextAmount.requestFocus();
              return;
          }
  
          class UpdateBorrowed extends AsyncTask&lt;Void, Void, Void&gt; {
  
              @Override
              protected Void doInBackground(Void... voids) {
                  //Creating an item
                  borrowed.setOwner(sOwner);
                  borrowed.setDesc(sDesc);
                  borrowed.setAmount(dAmount);
  
                  //Adding to database
                  DatabaseClient.getInstance(getContext())
                          .borrowedDao()
                          .update(borrowed);
                  return null;
              }
  
              //Returns to the {RecyclerViewFragment} because the item is created
              @Override
              protected void onPostExecute(Void aVoid) {
                  super.onPostExecute(aVoid);
  
                  DisplayBorrowed displayBorrowed = new DisplayBorrowed();
                  getChildFragmentManager().beginTransaction().replace(R.id.mainer, displayBorrowed).addToBackStack(null).commit();
                  Toast.makeText(getContext(), "Updated", Toast.LENGTH_LONG).show();
              }
          }
          UpdateBorrowed ut = new UpdateBorrowed();
          ut.execute();
      }
  
  
      private void deleteBorrowed(final Borrowed borrowed) {
          class DeleteBorrowed extends AsyncTask&lt;Void, Void, Void&gt; {
  
              @Override
              protected Void doInBackground(Void... voids) {
                  DatabaseClient.getInstance(getContext())
                          .borrowedDao()
                          .delete(borrowed);
                  return null;
              }
  
              //Returns to the {RecyclerViewFragment} because the item is created
              @Override
              protected void onPostExecute(Void aVoid) {
                  super.onPostExecute(aVoid);
  
                  DisplayBorrowed displayBorrowed = new DisplayBorrowed();
                  getChildFragmentManager().beginTransaction().replace(R.id.mainer, displayBorrowed).addToBackStack(null).commit();
                  Toast.makeText(getContext(), "Deleted", Toast.LENGTH_LONG).show();
              }
          }
          DeleteBorrowed dt = new DeleteBorrowed();
          dt.execute();
      }
  
  }
  </code></pre>
  
  <p><strong>Run log</strong></p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.umbrella.fragmenttest, PID: 31428
      java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference
          at com.umbrella.fragmenttest.UpdateBorrowedClass.onCreateView(UpdateBorrowedClass.java:31)
          at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
          at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
          at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
          at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
          at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
          at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
          at android.os.Handler.handleCallback(Handler.java:789)
          at android.os.Handler.dispatchMessage(Handler.java:98)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6541)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  Application terminated.
  </code></pre>