1

I have a method in my main activity that lets user logout if user is logged in and log in if user logged out. But the application crashes when I call .logout() method of facebook sdk. My code where the error comes Logcat returns NULLPOINTEREXCEPTION Error at fb.logout(MainActivity.this);

public void loginFacebook(View v) {
    // TODO Auto-generated method stub
    new Thread(new Runnable() {

        @Override
        public void run() {

            // log out if logged in
            if (fb.isSessionValid()) {
                try {
                    updateButtonImage();
                    Log.d("Session Valid", "Session Valid");
                    fb.logout(MainActivity.this);
                    if (fb.isSessionValid())
                        Log.d("Session Valid", "Session Valid");
                    else
                        Log.d("Session Not Valid", "Session Not Valid");

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            else {
                // log in if logged out
                Log.d("Session Not Valid", "Session Not Valid");
                // login
                runOnUiThread(new Runnable() {
                    public void run() {
                        fb.authorize(MainActivity.this,
                                new DialogListener() {

                                    // I have deleted this part as this is not relevant to this question
                                });

                    }
                });

            }
        }

    }).start();

}

LOGCAT:

    03-08 16:11:22.242: E/AndroidRuntime(11956): FATAL EXCEPTION: Thread-14
03-08 16:11:22.242: E/AndroidRuntime(11956): java.lang.NullPointerException
03-08 16:11:22.242: E/AndroidRuntime(11956):    at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:96)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:554)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:578)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at com.facebook.Session.closeAndClearTokenInformation(Session.java:798)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at com.facebook.android.Facebook.logoutImpl(Facebook.java:669)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at com.facebook.android.Facebook.logout(Facebook.java:646)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at com.appquest.awaaziitkgp.MainActivity$4.run(MainActivity.java:167)
03-08 16:11:22.242: E/AndroidRuntime(11956):    at java.lang.Thread.run(Thread.java:1019)
Harshil Pansare
  • 1,117
  • 1
  • 13
  • 37

1 Answers1

0

For some reason, the Facebook object fb is becoming null inside the run() method. You can try calling

session.closeAndClearTokenInformation();

instead which does the same thing. Or you can try to figure out why fb becomes null. I suspect it has something to do with the fact that you are running it on a separate Thread (and I don't really see the need for that).

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I had some code in that thread before that required loading some data from internet. I removed the thread and now its working fine. Thanks. – Harshil Pansare Mar 08 '15 at 11:40
  • However, I don't understand what was the problem. I had initialized fb variable in onCreate() as fb = new Facebook(APP_ID); – Harshil Pansare Mar 08 '15 at 11:40
  • I suspect that objects of the `Facebook` class are not *thread-safe*, in which case they cannot be accessed as class fields, they should be supplied in the constructor to that thread instead. This needs to be checked in the documentation. This type of problem is an example of the reason why the Android framework designers have adopted the single UI-thread policy, it lets the developers do their work without having to synchronize callbacks and fields ... – Yash Sampat Mar 08 '15 at 11:45