So I have an AsyncTask class to handle populating my RecyclerView
. It works well but when it needs to be refreshed then it crashes with a NullPointerException
. I kind of know why but then I can't do it because I am creating a new object
and adding it to my list
.
This is my code:
@Override
protected void onPostExecute(String s) {
try {
userslist.clear();
JSONArray jsonArray = new JSONArray(s);
for(int i=0; i < jsonArray.length(); i++){
...
UsersData usersData = new UsersData(var1, var2);
userslist.add(UsersData);
}
cAdapter.notifyDataSetChanged(); // Culprit line, despite the list being deleted and added again in the try block
} catch (JSONException e) {
e.printStackTrace();
}
}
I suspect is it something to do with the userslist
.
This is my error logs:
java.lang.NullPointerException
at lukazs.newapp.UserInfo$GetUserList.onPostExecute(UserInfo.java:218)
at android.os.AsyncTask.finish(AsyncTask.java:741)
at android.os.AsyncTask.access$600(AsyncTask.java:197)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:654)
at android.os.Handler.dispatchMessage(Handler.java:100)
EDIT: This is the method call where I populate the recyclerview
:
public void populateRecyclerList(){
GetUserList getUserList = new GetUserList();
getUserList.execute();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
setContentView(R.layout.usersdetails);
populateRecyclerList();
}
This is where cAdapter
is initialised, in onCreate
method:
RecyclerView.Adapter cAdapter;
ArrayList<UserDetailsProvider> userslist = new ArrayList<UserDetailsProvider>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = (RecyclerView) findViewById(R.id.recycleMusic);
cAdapter = new UserDefinedAdapter(userslist);
recyclerView.setAdapter(cAdapter);
populateRecyclerList();
}
EDIT: I have done something now, but the populateRecyclerList()
is crashing in another AsyncTask class
on onPostExecute
. Basically, I want to repopulate the recyclerView after a user has been added. This is my code for the onPostExecute()
method, where I am calling the populateRecyclerList()
method:
@Override
protected void onPostExecute(String s) {
populateRecyclerList();
}
Maybe you are not meant to call this method here? But then how would I update the RecyclerView
?