title: How to parse JSONObject retrieved from facebook in android using Java?
body: <p>I am trying to parse JSONObject returned from facebook graph API in java.
  I want to fetch some user data and friends' data through graph api.
  The graph I am trying to parse is this: </p>
  
  <pre><code>{
    "id": "985453561536987",
    "first_name": "abc",
    "last_name": "xyz",
    "name": "abc xyz",
    "birthday": "03/17/1995",
    "gender": "male",
    "friends": {
      "data": [
        {
          "first_name": "Div",
          "last_name": "Tol",
          "gender": "female",
          "id": "150534786284567"
        }
      ],
      "paging": {
        "next": "https://graph.facebook.com/v2.4/985363151536997/friends?fields=first_name,last_name,about,birthday,gender&amp;access_token=CAANEU8qO1HYBAJG4ZCYtc5lLuZBv3zKWe6qRN3Oy5iYtijFsQwplDH2AFe2YiXS1lZCXtxnYZBXLhBo6N65KEmW3wuNKxXiZCCVgFpg8r3elkqgCRFZBZAIgscoZCtxezk08fpiPnYqujCdMY4ZB3d88vqcDZCnpon9TjPZBE2Irzlrcqn8gzPZB8nvRu2ZBoLt6Y1yGlovgt0K18uYBbyj8GpNZCX&amp;limit=25&amp;offset=25&amp;__after_id=enc_AdBfWxVZCaZAlL5ZBGlZAQjMZC2M5m4uQMNSQPfwNfsxC9TwgQzgK55kQEVGeZBEJMoyXfvySnZAOwaHCGvmgj5Ngzut6D9"
      },
      "summary": {
        "total_count": 533
      }
    },
  }
  </code></pre>
  
  <p>I am getting NullPointerException while trying to parse the JSONObject
  For parsing above information I do following:</p>
  
  <pre><code>public void graphActivity(AccessToken accessToken) {
  
          //used is graph request code from graph api explorer
          GraphRequest request = GraphRequest.newMeRequest(
                  accessToken,
                  new GraphRequest.GraphJSONObjectCallback() {
                      @Override
                      public void onCompleted(JSONObject object, GraphResponse response) {
                          // Insert your code here
                          try {
  
                              //getting error in following code where object.getString("first_name");
                              //is written
                              //null pointer exception
  
                              //userVO.setId(object.getString("id");
                              userVO.setUserFirstName(object.getString("first_name"));
                              userVO.setUserLastName(object.getString("last_name"));
                              userVO.setUserName(object.getString("name"));
                              userVO.setUserBirthday(object.getString("birthday"));
                              userVO.setUserGender(object.getString("gender"));
  
  
                              JSONObject friendsJsonObject = object.getJSONObject("friends");
  
                              JSONArray data = friendsJsonObject.getJSONArray("data");
  
                              String userString = "USER::\n"
                                      + "id: " + userVO.getId() + "\n"
                                      + "fn: " + userVO.getUserFirstName() + "\n"
                                      + "ln: " + userVO.getUserLastName() + "\n"
                                      + "un: " + userVO.getUserName() + "\n"
                                      + "gen: " + userVO.getUserGender() + "\n"
                                      + "bday: " + userVO.getUserBirthday() + "\n";
  
                              Log.d(TAG, userString);
                              friendList = new ArrayList();
                              int length_list = data.length();
                              for (int i = 0; i &lt; length_list; i++) {
                                  JSONObject dataObj = data.getJSONObject(i);
                                  //friendVO.setFriendId(dataObj.getString("id"));
  
                                  friendVO.setFriendFirstName(dataObj.getString("first_name"));
                                  friendVO.setFriendLastName(dataObj.getString("last_name"));
                                  friendVO.setFriendName(dataObj.getString("name"));
                                  friendVO.setFriendBirthday(dataObj.getString("birthday"));
                                  friendVO.setFriendGender(dataObj.getString("gender"));
                                  friendList.add(friendVO);
  
                                  String friendString = "FRIEND::\n"
                                          + "id: " + friendVO.getFriendId() + "\n"
                                          + "fn: " + friendVO.getFriendFirstName() + "\n"
                                          + "ln: " + friendVO.getFriendLastName() + "\n"
                                          + "un: " + friendVO.getFriendName() + "\n"
                                          + "gen: " + friendVO.getFriendGender() + "\n"
                                          + "bday: " + friendVO.getFriendBirthday() + "\n";
  
                                  Log.d(TAG, friendString);
  
  
  
  
                              }
  
  
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
  
                      }
                  });
  
          Bundle parameters = new Bundle();
          parameters.putString("fields", "id,first_name,last_name,name,birthday,about,gender,website,friends{first_name,last_name,name,about,birthday,gender}");
          request.setParameters(parameters);
          request.executeAsync();
      }
  </code></pre>
  
  <p>Note: I am new to JSON parsing. Your help would be very useful, not just for this question but also for JSON Parsing. Thank you for reading. :)</p>
  
  <p>EDIT:
  errorlog:</p>
  
  <pre><code>10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ java.lang.NullPointerException
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at com.wordpress.toknowtoshare.fbintegrationexample.MainActivity$2.onCompleted(MainActivity.java:190)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:300)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at com.facebook.GraphRequest$5.run(GraphRequest.java:1364)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5021)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
  10-24 16:31:13.050    5266-5266/com.wordpress.toknowtoshare.fbintegrationexample W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
  </code></pre>