Swipe to delete Recyclerview brings about a null object reference error [duplicate]


Swipe to delete Recyclerview brings about a null object reference error [duplicate]



This question already has an answer here:



I am working with a recyclerview and am implementing swipe to delete. the code works pretty well but when I try getting my list from the adapter so that I implement swipe to delete, I get a null object reference.
My adapter class is below


public class TlAdapter extends RecyclerView.Adapter<TimelineVholder> {
private List<FetchModel> models;
private Context context;


public TlAdapter(List<FetchModel> models,Context context) {
this.models = models;
this.context= context;
}

@NonNull
@Override
public TimelineVholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view= LayoutInflater.from(context).inflate(R.layout.timeline,parent,false);

return new TimelineVholder(view,viewType);
}

@Override
public void onBindViewHolder(@NonNull TimelineVholder holder, final int position) {
SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor=preferences.edit();

FetchModel list= models.get(position);

String activity= list.getJournalEntry();
String time= list.getTime();

holder.activity.setText(activity);
holder.time.setText(time);
}

@Override
public int getItemViewType(int position) {
return TimelineView.getTimeLineViewType(position,getItemCount());
}
@Override
public int getItemCount() {

int arr=0;
try {
if (models.size()==0){
arr=0;
}
else {
arr=models.size();
}
}
catch (Exception e){}
return arr;
}

public List<FetchModel> getkey(){
return models;
}



}



and my recyclerview gesture class is as below


public class RecyclerViewBasic {
private Context context;
private List<FetchModel> modelList;
public RecyclerViewBasic(Context context) {
this.context = context;
}
public void recyclerGesture(){
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.RIGHT|ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView,
RecyclerView.ViewHolder
viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int
direction) {
int position= viewHolder.getAdapterPosition();

TlAdapter adapter= new TlAdapter(modelList,context);

modelList= adapter.getkey();
modelList.remove(position);
adapter.notifyDataSetChanged();

}
}).attachToRecyclerView(recyclerView);

}



and the error in my logcat is as below also


06-29 13:19:41.691 4282-4282/com.revosleap.journal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.revosleap.journal, PID: 4282
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.remove(int)' on a null object reference
at com.revosleap.journal.RecyclerUtils.RecyclerView.RecyclerViewBasic$2.onSwiped(RecyclerViewBasic.java:89)
at android.support.v7.widget.helper.ItemTouchHelper$4.run(ItemTouchHelper.java:692)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)



I have gone through a number of similar questions here and tried the methods suggested but I cannot get the one that helps me out. I even tried to populate the list anew but it even got me another error. I'd really appreciate any possible help. Thanks.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Please post your full logcat.
– Gokul Nath KP
Jun 29 at 10:18





Already posted pls check
– Carlos Anyona
Jun 29 at 10:22





This is not complete logs. There will be a line with message "Caused by..."
– Gokul Nath KP
Jun 29 at 10:28




1 Answer
1



You have not initialized modelList. So accept one more parameter in the constructor of RecyclerViewBasic and initialize the list.


modelList





I have done it and the error still persists.
– Carlos Anyona
Jun 29 at 10:35





So every time onSwiped is called a new object of adapter is being created which should not be the case. There should only be one object of the adapter.
– Abdul Wadood
Jun 29 at 10:38



onSwiped





You can create the object of the adapter where you are binding the adapter with the recycler view and pass that object to the constructor of RecyclerViewBasic as another parameter.
– Abdul Wadood
Jun 29 at 10:43

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift