Rooky Coder Rooky Coder - 1 month ago 14
Android Question

Getting Null Pointer Exception - Using an Interface to Pass Data from an Adapter to a Sub-fragment

I have an

and a
. I am using an interface to pass data from the
to the
. The problem I am having is that I keep getting a
Null Pointer Exception
. I have read through the various posts here relating to the problem and can't figure out where I went wrong. I get the NPE at the line 'passAdapterVariable.passAdapterVariable(mname)'. Based on what I have read I suspect it may be because I am not properly initializing passAdapterVariable. I have tried initializing it several different ways based on other examples but I keep getting the NPE.

Here is the Adapter

public class MatchAdapter extends RecyclerView.Adapter<MatchAdapter.MatchViewHolder> {

//declaration of variables
private Fragment fragment;
private FragmentManager fragmentManager;
private DiscoverPage discoverPage;
private Context context;
private int size;
private int mposition;
private TextView txt_matchname;
private ImageView img_matchpic;
List<String> maImg = new ArrayList<>();
private String mname;
PassAdapterVariable passAdapterVariable;

public interface PassAdapterVariable {

void passAdapterVariable(String mname);

//the constructor
public MatchAdapter(List<String> maImg, int size, Context context, DiscoverPage discoverPage){//, PassAdapterVariable passAdapterVariable) {

this.maImg = maImg;
this.context = context;
this.discoverPage = discoverPage;
this.size = size;
//this.passAdapterVariable = (PassAdapterVariable)context;

public MatchAdapter(String mname, Context context) {
this.context = context;
this.passAdapterVariable = (PassAdapterVariable)context;

//PassAdapterVariable passAdapterVariable = (PassAdapterVariable) context;

public MatchAdapter.MatchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.match_items, parent, false);
MatchViewHolder matchViewHolder = new MatchViewHolder(view, maImg, discoverPage);

return matchViewHolder;

public void onBindViewHolder(MatchViewHolder holder, int position) {



public int getItemCount() {
return maImg.size();

//viewholder class
public class MatchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private static final String TAG = "error";
//declare variables
private DiscoverPage discoverPage;
private ImageView img_match;

//ViewHolder constructor
public MatchViewHolder(View itemView, final List<String> maImg, final DiscoverPage discoverPage) {

//initialize variables inside the viewholder constructor
this.discoverPage = discoverPage;
img_match = (ImageView) itemView.findViewById(;
txt_matchname = (TextView) itemView.findViewById(;
img_matchpic = (ImageView) itemView.findViewById(;

//set click listener for the img_match


public void onClick(View view) {
if (view == img_match) {
Fragment currentFragment;
fragment = new ClickedMatch();
fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(, fragment);

if ((currentFragment = ((AppCompatActivity) context).getSupportFragmentManager().findFragmentById( != null) {
else {

mname = maImg.get(getAdapterPosition());
mposition = getAdapterPosition();
mname = maImg.get(mposition);



Here is the Sub-Fragment

public class ClickedMatch extends Fragment implements MatchAdapter.PassAdapterVariable{

//declare variables
private Toolbar toolbar;
private TextView txt_matchname;
private TextView txt_matchprice;
private ImageView img_matchpic;
private String mname;
private String imgmatch;
List<String> maImg = new ArrayList<>();
int size;
Context context;
DiscoverPage discoverPage;
private String pname;
private int i;
MatchAdapter.PassAdapterVariable passAdapterVariable;

public ClickedMatch() {
// Required empty public constructor


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_clicked_match, container, false);

//initialize variables
toolbar = (Toolbar) view.findViewById(;
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);//set toolbar as action bar
txt_matchname = (TextView)view.findViewById(;
img_matchpic = (ImageView)view.findViewById(;


toolbar.setNavigationOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

if(((AppCompatActivity)getActivity()).getSupportActionBar()!= null){
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// MatchAdapter matAdapter = new MatchAdapter(maImg, size, context, discoverPage,

// matAdapter.passAdapterVariable = this;

MatchAdapter matAdapter = new MatchAdapter(pname, getContext());
matAdapter.passAdapterVariable = this;

return view;
public void passAdapterVariable(String mname) {
this.pname = mname;


public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == {

return super.onOptionsItemSelected(item);



Here is the error log

08-13 21:53:49.341 12852-12852/com.test.jack E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.jack, PID: 12852

java.lang.NullPointerException: Attempt to invoke interface method 'void com.test.jack.MatchAdapter$PassAdapterVariable.passAdapterVariable(java.lang.String)' on a null object reference
at com.test.jack.MatchAdapter$MatchViewHolder.onClick(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

To add some more context, I have a main activity(UserMainPage). A bottom navigation menu selector replaces the UserMainPage with a fragment(DiscoverPage).

DiscoverPage calls the adapter(MatchAdapter). A button click on DiscoverPage replaces DiscoverPage with a sub-fragment(ClickedMatch).

I am trying to pass a variable from MatchAdapter to ClickedMatch.

Answer Source

I was able to find a solution with no help from those who commented or provided insight to this question and had only served to demonstrate their ineptness and waste my time.

I was able to pass data from an adapter to a sub-fragment using a Bundle.

I modified my code as follows:

In the adapter I added:

fragment = new ClickedMatch();
Bundle bundle = new Bundle();



In my sub-fragment I added:

Bundle bundle = this.getArguments();
if(bundle != null){
   pname = bundle.getString("matchname");

There was no need to use an interface. Based on what I have researched I think (and I may be wrong) that creating an interface and then implementing it is better used when communicating between fragments as opposed to communicating between an adapter and a sub-fragment.