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

Adapter
and a
Sub-Fragment
. I am using an interface to pass data from the
Adapter
to the
Sub-Fragment
. 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;


@Override
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;
}


@Override
public void onBindViewHolder(MatchViewHolder holder, int position) {

Picasso.with(context).load(maImg.get(position)).into(holder.img_match);
holder.setIsRecyclable(false);

}


@Override
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) {
super(itemView);

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


//set click listener for the img_match
img_match.setOnClickListener(this);

}


@Override
public void onClick(View view) {
if (view == img_match) {
//discoverPage.isHidden();
Fragment currentFragment;
fragment = new ClickedMatch();
fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(android.R.id.content, fragment);
transaction.addToBackStack("DiscoverPage");

if ((currentFragment = ((AppCompatActivity) context).getSupportFragmentManager().findFragmentById(R.id.main_container)) != null) {
transaction.hide(currentFragment);
}
else {
transaction.commit();
}


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

}

}
}
}


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

}

@Override
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(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);//set toolbar as action bar
txt_matchname = (TextView)view.findViewById(R.id.txt_matchname);
img_matchpic = (ImageView)view.findViewById(R.id.img_matchpic);

//setHasOptionsMenu(true);

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});

if(((AppCompatActivity)getActivity()).getSupportActionBar()!= null){
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("");
}

// MatchAdapter matAdapter = new MatchAdapter(maImg, size, context, discoverPage,
//passAdapterVariable);

// matAdapter.passAdapterVariable = this;
//passAdapterVariable.passAdapterVariable(mname);
//txt_matchname.setText(pname);

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

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

}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
this.getActivity().finish();
}

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(MatchAdapter.java:138)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


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();
bundle.putString("matchname",mname);
fragment.setArguments(bundle); 

...

transaction.commit();

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.