Android app facebook login : NULL pointer exception

2.6k views Asked by At

My problem is somewhat like these only... First and Second Question. I've tried everything given on these posts but to no relief.

So I am using android-simple-facebook library to create a facebook login. Here's my LoginActivity.java

public class LoginActivity extends Activity {
    private Button mButtonLogin;
    private SimpleFacebook mSimpleFacebook;
    protected static final String TAG = LoginActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mSimpleFacebook = SimpleFacebook.getInstance();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mButtonLogin = (Button) findViewById(R.id.button_login);
        setLogin();
    }

    public void setLogin() {

        final OnLoginListener onLoginListener = new OnLoginListener() {
            @Override
            public void onLogin() {
                Log.i(TAG, "Logged in");
            }
        };

        mButtonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mSimpleFacebook.login(onLoginListener);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
       // mSimpleFacebook = SimpleFacebook.getInstance();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data); 
        super.onActivityResult(requestCode, resultCode, data);
    }


}

But as soon as i click the login button, the app crashes with NullPointerException. Here's the LogCat

09-21 15:54:40.109: E/AndroidRuntime(21895): FATAL EXCEPTION: main
09-21 15:54:40.109: E/AndroidRuntime(21895): Process: www.kb.com, PID: 21895
09-21 15:54:40.109: E/AndroidRuntime(21895): java.lang.NullPointerException
09-21 15:54:40.109: E/AndroidRuntime(21895):    at www.kb.com.LoginActivity$2.onClick(LoginActivity.java:81)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at android.view.View.performClick(View.java:4438)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at android.view.View$PerformClick.run(View.java:18422)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at android.os.Handler.handleCallback(Handler.java:733)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at android.os.Looper.loop(Looper.java:136)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at java.lang.reflect.Method.invoke(Method.java:515)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-21 15:54:40.109: E/AndroidRuntime(21895):    at dalvik.system.NativeStart.main(Native Method)
2

There are 2 answers

3
Stefan Freitag On

Please check if you are using the correct API-call. The API documentation states for getInstance()

/**
 * Get the instance of {@link SimpleFacebook}. <br>
 * <br>
 * <b>Important:</b> Use this method only after you initialized this library
 * or by: {@link #initialize(Activity)} or by {@link #getInstance(Activity)}
 * 
 * @return The {@link SimpleFacebook} instance
 */
public static SimpleFacebook getInstance() {
    return mInstance;
}

I did not see a call like initialize(Activity) or getInstance(Activity) in your code snippet.

0
suneet On

As it turns out there was nothing wrong with the code or the library, but was an issue with android support lib. I finally found the solution here