-2

i'm using volley library to receive data from json file I have this problem i don't know the reason for this error

Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  private void fetchRemoteData(final DataStatus callback){
    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {

                    List<Post> listItems = new ArrayList<>();
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        JSONArray array = jsonObject.getJSONArray("bgs");
                        for (int i = 0; i < array.length(); i++){
                            JSONObject o = array.getJSONObject(i);
                            Post item = new Post(
                                    o.getString("img")
                            );
                            listItems.add(item);
                        }
                        callback.onSuccess(listItems);
                    } catch (JSONException e) {
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    callback.onError(error);
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);
}

error exist here :

@Override
                public void onErrorResponse(VolleyError error) {
                    callback.onError(error);
                }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
S.Queroane
  • 77
  • 2
  • 12

1 Answers1

1

Update this line:

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

with

RequestQueue requestQueue = Volley.newRequestQueue(getContext());

or

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
  • 1
    actually i'm working with fragments so i cant add `getApplciationConetxt() ` so what i should i do which one i should add `getContext()` or `getActivity().getApplciationConetxt()` – S.Queroane Oct 15 '17 at 13:07