fruity
fruity

Reputation: 13

Getting null pointer exception while sharing on Facebook

I am using the following code for FaceBook share.The picture is posted in some FaceBook ID but for some certain FaceBook ID I am getting Null Pointer Exception .How could i resolve this issue.

   postParams.putString("message", message);
   postParams.putString("picture",picture);
   System.out.println("******* 111111 *********");

   com.facebook.Request.Callback callback = new  com.facebook.Request.Callback()
    {
     public void onCompleted(Response response)
      {
         JSONObject graphResponse =response.getGraphObject().getInnerJSONObject();
         String postId = null;
         try {
         postId = graphResponse.getString("id");
         } catch (JSONException e) 
         {
         Log.i("postthis", "JSON error " + e.getMessage());
         }
         FacebookRequestError error = response.getError();
         if (error != null) {
            Log.i("postthis", "JSON error "     +error.getErrorMessage());
            } 
         else {
//       Toast.makeText(ctx.getApplicationContext(), postId,
//                       Toast.LENGTH_LONG).show();
          }
          }
          };

this is logcat error :

01-22 09:43:20.812: E/AndroidRuntime(5201): FATAL EXCEPTION: main
01-22 09:43:20.812: E/AndroidRuntime(5201): java.lang.NullPointerException
01-22 09:43:20.812: E/AndroidRuntime(5201):     at FBLogin$2.onCompleted  (FBLogin.java:316)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at com.facebook.Request$4.run(Request.java:1669)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.os.Handler.handleCallback(Handler.java:615)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.os.Looper.loop(Looper.java:137)
01-22 09:43:20.812: E/AndroidRuntime(5201):     at android.app.ActivityThread.main(ActivityThread.java:4895)
01-22 09:43:20.843: E/android.os.Debug(353): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

Upvotes: 1

Views: 1652

Answers (3)

priyanka morisetti
priyanka morisetti

Reputation: 267

check your parameters , if any one of the parameter is null then the graph object shows null value in facebook sharing.

Upvotes: 1

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

Once try this this is working for me in Android sdk 3.x

 void publishToWall(Session session) {      
    if (session != null)  {          
        Bundle postParams = new Bundle();
        postParams.putString("message", "contentStr");
        postParams.putString("description",  "description");
        postParams.putString("link", "");
        postParams.putString("name", "Name");
        postParams.putString("picture", "picture url");

        Request.Callback callback = new Request.Callback()  {
            public void onCompleted(Response response)  {       

            }
        };
        Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();             

        Log.i("TAG", "Message posted to your facebook wall!..");         


    }           
 }

Upvotes: 0

Abhishek Agarwal
Abhishek Agarwal

Reputation: 1897

The session for posting on wall is null so there is null pointer exception you have to active your session first for posting on wall. You can use the following code for sharing on wall.

   Session.openActiveSession(activity, true, new Session.StatusCallback() {

    // callback when session changes state
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        if (session != null && session.isOpened()) {

            // Check for publish permissions
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                        activity, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest);
                return;
            }

            Bundle postParams = new Bundle();
            postParams.putString("message", message);

                postParams.putString("tags",tag);
                     postParams.putString("place",place_id);



            Request.Callback callback = new Request.Callback() {
                private String toastmessage;

                public void onCompleted(Response response) {
                    try {
                        JSONObject graphResponse = response
                                .getGraphObject().getInnerJSONObject();
                        String postId = null;

                        postId = graphResponse.getString("id");
                    } catch (Exception e) {
                        Log.i("Test", "JSON error " + e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        isPosted(false);
                        Toast.makeText(
                                activity.getApplicationContext(),
                                error.getErrorMessage(),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        isPosted(true);
                        toastmessage = "Posted Successfully";
                        Toast.makeText(activity, toastmessage,
                                Toast.LENGTH_SHORT).show();
                        {

                        }
                    }
                }
            };

            Request request = new Request(session, "me/feed",
                    postParams, HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }


    }
});

Given at this link How do I publish a check in to facebook with android SDK

Upvotes: 0

Related Questions