Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VolleyPlus : NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException

I am trying to run below code:

Map<String, Object> requestMap = new HashMap<>();
Long unixTime = System.currentTimeMillis() / LONG_1000;
requestMap.put(KEY_TIME, unixTime);
JWTSigner signer = new JWTSigner(SECRET);
String token = signer.sign(requestMap);
String url = BASE_URI + "/Data/Categories?d=" + token;
StringRequest stringRequest =  new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, error.getLocalizedMessage());
    }
});

RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
mRequestQueue.add(stringRequest);

But I am getting following error:

[6056] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
    at com.android.volley.toolbox.HttpHeaderParser.parseCharset(HttpHeaderParser.java:243)
    at com.android.volley.toolbox.HttpHeaderParser.parseCharset(HttpHeaderParser.java:262)
    at com.android.volley.request.StringRequest.parseNetworkResponse(StringRequest.java:70)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:133)
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference

I have no idea what is to problem. Can anyone help?

Thanks in advance.

like image 303
Olcay Ertaş Avatar asked Oct 08 '15 13:10

Olcay Ertaş


3 Answers

You need to override the parseNetworkResponse method in StringRequest and fix a vulnerability which does not anticipate a null header map:

StringRequest stringRequest =  new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, error.getLocalizedMessage());
    }
}) {
    @Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
        if (response.headers == null)
        {
            // cant just set a new empty map because the member is final.
            response = new NetworkResponse(
                               response.statusCode,
                               response.data,
                               Collections.<String, String>emptyMap(), // this is the important line, set an empty but non-null map.
                               response.notModified,
                               response.networkTimeMs);


        }

        return super.parseNetworkResponse(response);
    }
};

Hope this helps.

like image 191
Gil Moshayof Avatar answered Nov 13 '22 20:11

Gil Moshayof


I encountered this problem because cidEDTValue was null.

{
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("CID", cidEDTValue);
        return params;
    }
}
like image 37
Nana medo Avatar answered Nov 13 '22 21:11

Nana medo


I traced the stacktrace and I believe that the root cause is response.headers is null in the method parseNetworkResponse for some reason. I would suggest the following to confirm this.

  1. Create a class that extends StringRequest
  2. In this class override the 'parseNetworkResponse()`. It should look as follows:

       protected Response<String> parseNetworkResponse(NetworkResponse response) {
              Log.d("Response Header = " + response.headers);
              super.parseNetworkResponse(response);
       }
    
  3. Use this class instead of StringRequest.

Once done, see if the headers is null or not. That should narrow down the problem. By the way, which version of Volley are you using. I never encountered such a problem.

like image 1
Henry Avatar answered Nov 13 '22 21:11

Henry