0

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.

Vítor Kim
  • 21
  • 5
  • What is line 109 in MatchRequestActivity.java? – Sloganho Jul 16 '15 at 15:20
  • In line 109 I try to use the user's name retrieved from the GET request: – Vítor Kim Jul 17 '15 at 13:37
  • getSupportActionBar().setTitle(userB_match.getName() + "'s Profile"); – Vítor Kim Jul 17 '15 at 13:37
  • Either getSupportActionBar() or getName() is throwing the nullpointer exception. This is you're error in the stack trace Caused by: java.lang.NullPointerException at com.pubmobile.ellow.Controller.Main.MatchFragment.MatchRequestActivity.onCreate(MatchRequestActivity.java:109) – Sloganho Jul 17 '15 at 13:59
  • If you would like to post all of your code for MatchRequestActivity.java it will be easier to help. – Sloganho Jul 17 '15 at 13:59
  • Thanks, @Sloganho! I'll do just that in a moment! Because I'm sure the problem is in the JSONObjectRequest... I was able to print the error from the GET request: **com.android.volley.NoConnectionError: java.io.EOFException**. Any ideas? – Vítor Kim Jul 17 '15 at 14:05

2 Answers2

0

When I copied and pasted your code into Android Studio it shows an error message (See below. Your code is the bottom request.) I reworked it and added the parameter you were missing and it doesn't throw the error anymore. I'm not positive if that's your error. You're getting a null pointer exception on line 109 of MatchRequestActivity.java but haven't disclosed what is on that line. Check this and see if it works.

        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");
        }
    });

Screenshot of compared requests

Sloganho
  • 2,041
  • 3
  • 16
  • 20
  • Hi, @Sloganho and thank you for your answer. I added the parameter (also had to cast it to String) but the same error is still shown : / – Vítor Kim Jul 17 '15 at 13:42
0

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;
    }
};
Vítor Kim
  • 21
  • 5