8

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.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
  • @blackbelt I know what is a NullPointerException. If you notice this exception doesn't come from my code directly. This is a VolleyPlus specific problem. So marking this question as duplicate is wrong. – Olcay Ertaş Oct 08 '15 at 14:05
  • What is `JWTSigner` ? – Blackbelt Oct 08 '15 at 14:06
  • Java Web Token library. https://github.com/auth0/java-jwt – Olcay Ertaş Oct 08 '15 at 14:06
  • 1
    I donot know exactly but this is the exception because of parsenetworkresponse . Try override parsenetworkresponse method in StringRequest. This is not exactly the same but this may help http://stackoverflow.com/questions/19267616/why-does-volleys-response-string-use-an-encoding-different-from-that-in-the-res – mubeen Oct 16 '15 at 19:12

4 Answers4

9

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.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
2

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;
    }
}
Adrien Lacroix
  • 3,502
  • 1
  • 20
  • 23
Nana medo
  • 263
  • 2
  • 6
  • This is so true. I had 8 params.put("CID", cidEDTValue) of which one of them was null. So what I did was to output each params using Toast. I got a blank toast at params(5) which signify that the value being passed was null. – Kenny Dabiri Aug 31 '17 at 17:46
1

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.

Henry
  • 17,490
  • 7
  • 63
  • 98
-2

I wrote this on build.gradle.dependencies.

compile 'com.android.volley:volley:1.0.0'

And it worked.