NullPointerException : Attempt to read from field ‘com. android.volley.Cache$Entry com.android.volley.Response.cacheEntry
I’m using Android Volley to get JSONArray from an external API. My code is below
StringRequest jq = new StringRequest("http://api.example.com/json/States", new Response.Listener<String>() {
@Override
public void onResponse(String jsonArray) {
try {
Log.d("Success", jsonArray.toString());
} catch (Exception je) {
je.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("Error", volleyError.toString());
}
}) {
@Override
protected Response parseNetworkResponse(NetworkResponse networkResponse) {
return null;
}
};
AppController a = AppController.getInstance();
Log.d("Queue", "Add");
a.addToRequestQueue(jq);
I keep getting the same error below
NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to read from field ‘com.android.volley.Cache$Entry com.android.volley.Response.cacheEntry’ on a null object reference
java.lang.NullPointerException: Attempt to read from field ‘com.android.volley.Cache$Entry com.android.volley.Response.cacheEntry’ on a null object reference
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:126)
I tried various methods. But there is still the same problem. I don’t understand what the problem is. Help solve this problem
Solution
You did not parse the response. You only return null instead of resolving the cache header. After parsing the response, you can parse the response with the cached header like this:
return Response.success(networkResponse, HttpHeaderParser.parseCacheHeaders(networkResponse));
Return it in the parseNetworkResponse(NetworkResponse networkResponse) method