Search code examples
multithreadingandroid-volleyjsonuncaught-exceptionget-request

JSONObjectRequest with Volley throws "W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a6c9d8)"


I'm new to Android development and to SO, I just started couple months ago. You guys have been a huge help so far! I'm using as many external libraries as I can.

I'm using Volley and JSONObject to make a GET request to the server. The problem is

W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a6c9d8)

is thrown and I get a NPE when trying to use the object from response.

This does not happen the other times I use JSONObjectRequest, just this one. I've researched and found something about the android:name field in the application tag inside AndroidManifest.xml. I don't think that's it.

I suspect it's about making the request in a different thread, but yet why doesn't it happen the other times...?

Well, my logcat and code are below. Please help me!!!

logcat:

07-16 02:01:37.466  22635-22635/com.revmob.android.setup W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a6c9d8)
07-16 02:01:37.476  22635-22635/com.revmob.android.setup E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.revmob.android.setup/com.pubmobile.ellow.Controller.Main.MatchFragment.MatchRequestActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1975)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4443)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.pubmobile.ellow.Controller.Main.MatchFragment.MatchRequestActivity.onCreate(MatchRequestActivity.java:109)
        at android.app.Activity.performCreate(Activity.java:4668)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4443)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)

My code: MatchRequestActivity.java

String url = Constants.URL_SERVER;
JsonObjectRequest getMatchRequest = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, new com.android.volley.Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        if(response != null) {

            System.out.println(response);

        } else{
            System.out.println("null Response from getMatchRequest");
        }
    }
}, new com.android.volley.Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("no Response from getMatchRequest");
    }
});
RequestQueueManager.getInstance(this).addToRequestQueue(getMatchRequest);

What I have right now:

Although my server is sending the response just fine, my code doesn't even enter onResponse() and throws the error. Then the NPE due to the lack of response.


Solution

  • I solved my problem with a simple fix: I had to add headers to my request.

    More specifically, "accept-encoding" headers with the string "".

    The code below:

    JsonObjectRequest getMatchRequest = new JsonObjectRequest(Request.Method.GET,
            url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            if(response != null) {
    
                System.out.println(response);
    
            } else{
                System.out.println("null Response from getMatchRequest");
            }
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("no Response from getMatchRequest");
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("accept-encoding", "");
            return headers;
        }
    };