deercoder/FitbitApp

NullPointerException: When request the activity information using Json Libs

deercoder opened this issue · 2 comments

When request some activity information like calorie, it seems that the callback function has null parameter, which cause the code crashing, as follows:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.uwanttolearn.easysocialfacebooktesting, PID: 7690
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
 at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.uml.deercoderi.fitbitmodule.EasySocialFitbit$1.requestComplete(EasySocialFitbit.java:100)
 at com.uwanttolearn.easysocial.webrequests.GetWebRequest$LocalAsyncTask.onPostExecute(GetWebRequest.java:64)
at com.uwanttolearn.easysocial.webrequests.GetWebRequest$LocalAsyncTask.onPostExecute(GetWebRequest.java:42)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5432)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
I/Process: Sending signal. PID: 7690 SIG: 9

The code snippets are located here:(but should be caused anywhere else)

    public void getUserInfo(Context context, final UserInfoCallback userInfoCallback){

        GetWebRequest getWebRequest = new GetWebRequest(new WebRequest.Callback() {
            @Override
            public void requestComplete(String line) {
                try {
                    Log.e("EasySocialFitbit", "callback line " + line);
                    JSONObject jsonObject = new JSONObject(line);
                    userInfoCallback.onComplete(jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                    userInfoCallback.onComplete(null);
                }
            }
        });
        getWebRequest.executeRequest(_EasySocialFitbitUrlManager.getUserInfoUrl(context));
    }

According to the logs in here, the process is like this:

GetUserInfo clicked -> GetWebRequest::doInBackground -> GetWebRequest::onPostExecute() -> getUserInfor::requestComplete() finishes.

In the onPostExecute(), it gets the parameters result, which will be passed to the last requestComplete, so it shows null.

This is caused by the connection.getInputStream(), not by the inputStreamParse(), otherwise it will print out the log in inputStreamParse().

Let's see why that will cause IOException....

This sequence of calling and lots of exception made it misleading when locating the error...

the uncessary code of GET request, don't set the output, and don't return null in the parseInputStream.