user3227070 user3227070 - 3 months ago 9
Android Question

Recyclerview on click null object reference

My adaptor:

public class Adaptor extends RecyclerView.Adapter<Adaptor.Holder>{

private ArrayList<Winkel> winkels;
private LayoutInflater inflater;
private ImageView icon;
private ItemCLickCallback itemCLickCallback;

public interface ItemCLickCallback {
void onItemClick(int p);
void onSecItemClick(int p);

}

public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){
this.itemCLickCallback = itemCLickCallback;
}

public Adaptor (ArrayList<Winkel> winkels,Context c){
this.inflater = LayoutInflater.from(c);
this.winkels = winkels;
}


@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.card_item, parent, false);
return new Holder(view);
}

@Override
public void onBindViewHolder(Holder holder, int position) {
Winkel winkel = winkels.get(position);
holder.title.setText(winkel.getNaam());
if (winkel.isFavourtite()){
holder.icon.setImageResource(R.drawable.ic_star_black_18dp);
}
else{
holder.icon.setImageResource(R.drawable.ic_star_border_black_18dp);
}

}

@Override
public int getItemCount() {

return winkels.size();
}

class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

private TextView title;
private View container;
private ImageView icon;

public Holder(View itemView) {
super(itemView);

title = (TextView)itemView.findViewById(R.id.lbl_item_text);
container = itemView.findViewById(R.id.cont_item_root);
icon = (ImageView) itemView.findViewById(R.id.im_item_icon_secondary);
icon.setOnClickListener(this);
container.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if(v.getId()==R.id.cont_item_root){
Log.d("lol",Integer.toString(getAdapterPosition()));
itemCLickCallback.onItemClick(getAdapterPosition());

}
else{
itemCLickCallback.onSecItemClick(getAdapterPosition());
}
}
}
}


My overview fragment:

@Override
public void onItemClick(int p) {
Log.d("lol",(Integer.toString(p)));
Winkel winkel = (Winkel) mijnwinkels.get(p);

Bundle detailsBundle = new Bundle();
detailsBundle.putString(DETAILS_NAAM,winkel.getNaam());
detailsBundle.putString(DETAILS_ADRES,winkel.getAdres());
detailsBundle.putString(DETAILS_DEELGEMEENTE,winkel.getDeelGemeente());
detailsBundle.putString(DETAILS_GEMEENTE,winkel.getGemeente());
detailsBundle.putString(DETAILS_POSTCODE,winkel.getPostcode());

Details detail= new Details();
detail.setArguments(detailsBundle);
this.getFragmentManager().beginTransaction()
.replace(R.id.details,detail,null)
.addToBackStack(null)
.commit();
}


Error message:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.hoofdgebruiker.winkelskortrijk.Utill.Adaptor$ItemCLickCallback.onItemClick(int)' on a null object reference.


My code is working up to the point where I get me recyclerview, the items are in it, but I can't click any of them without getting this error and I don't understand why. What am I doing wrong that it is a null object reference? Neither of my logs are showing up.

Edit: my method onItemCLickCallback is used here:

@Override
public void onSuccess(ArrayList<Winkel> winkels) {
mijnwinkels = winkels;
Adaptor adaptor = new Adaptor(mijnwinkels, this.getActivity());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
mRecyclerView.addItemDecoration(new VerticalSpace(30));
mRecyclerView.setAdapter(adaptor);
adaptor.setItemCLickCallback(this);
}

Answer

There is a typo in your method arguments

public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){
    this.itemCLickCallback = itemCLickCallback;
}

You are expecting itemCLickCallback1 and assigning itemCLickCallback (notice the difference of extra '1' as suffix).