Android RecyclerView Does Not Update

I am trying to add some values to my recyclerView adapter which I took from JSON. I am taking values correctly but it is giving this log:

java.lang.NullPointerException:Attempt to invoke virtual method
‘int java.util.ArrayList.size()’ on a null object reference at: com.example.asus.drawer_app.MyAdapter.getItemCount(

My adapter is defined follow:

public class MyAdapter extends RecyclerView.Adapter {

private ArrayList itemsData;

public MyAdapter(ArrayList<ItemData> itemsData) {
    this.itemsData = itemsData;


// Create new views (invoked by the layout manager)
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_news, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;

// Replace the contents of a view (invoked by the layout manager)
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    // - get data from your itemsData at this position
    // - replace the contents of the view with that itemsData



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

// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtViewDate;
    public TextView txtViewTitle;
    public TextView txtViewDescription;
    public TextView imgViewIcon;
    public ViewHolder(View itemLayoutView) {
        txtViewTitle = (TextView) itemLayoutView.findViewById(;
        txtViewDate = (TextView) itemLayoutView.findViewById(;
        txtViewDescription = (TextView) itemLayoutView.findViewById(;
        imgViewIcon = (TextView) itemLayoutView.findViewById(;

// Return the size of your itemsData (invoked by the layout manager)


And my fragment code is :

public class NewsFragment extends Fragment{

String getItemsUrl="";
ArrayList<ItemData> itemsData = null;

public void onCreate(Bundle savedInstanceState) {

    new JSONGetNews().execute();

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_news, container, false);

    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(;

    // 2. set layoutManger
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    // 3. create an adapter

    MyAdapter mAdapter = new MyAdapter(itemsData);

    // 4. set adapter

    // 5. set item animator to DefaultAnimator
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    return rootView;

private class JSONGetNews extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog pDialog;

    protected void onPreExecute() {
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Data ...");


    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(getItemsUrl);
        return json;

    protected void onPostExecute(JSONObject json) {
        try {
            // Getting JSON Array
            JSONArray titleArr = json.getJSONObject("data").getJSONArray("results");

            for (int i = 0; i < titleArr.length(); i++) {
                final JSONObject titleObj = titleArr.getJSONObject(i);
                String title = titleObj.getString("title");
                String description = titleObj.getString("description");
                String imgUrl = titleObj.getString("img");
                String date = titleObj.getString("date");

           itemsData.add(new ItemData(title, date, imgUrl, description));

            }catch(JSONException e){


I think itemsData is NULL. If it is null, how can I send it to adapter? And how can I update adapter.?

Thanks For Help.

Source: java

Leave a Reply