0

I have created a function to login using Facebook.

public void Login ()
{
    var ctx = Forms.Context as MainActivity;
    var accounts = 
        new List<Account>(AccountStore.Create (ctx).FindAccountsForService (SERVICE));
    if (accounts.Count == 1) {
        GetAccount (accounts [0]);
        return;
    }

    var auth = new OAuth2Authenticator (
        clientId: FBID,
        scope: string.Empty,
        authorizeUrl: new Uri (AUTH_URL),
        redirectUrl: new Uri (REDIRECT_URL));

    auth.Completed += (sender, eventArgs) => {
        AccountStore.Create (ctx).Save (eventArgs.Account, "Facebook");
        GetAccount (eventArgs.Account);
    };
    ctx.StartActivity (auth.GetUI (ctx));
}

The thing is that after I enter my credentials in the FB login page, an exception is thrown before it reaches the Completed event.
I've downloaded the Xamarin.Auth project from GitHub trying to debug the program, but it doesn't break at the breakpoint unfortunately.

Caused by: JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Auth.OAuth2Authenticator.OnRetrievedAccountProperties (System.Collections.Generic.IDictionary`2) [0x00017] in d:\Downloads\Xamarin.Auth-master\Xamarin.Auth-master\src\Xamarin.Auth\OAuth2Authenticator.cs:373
at Xamarin.Auth.OAuth2Authenticator.OnRedirectPageLoaded (System.Uri,System.Collections.Generic.IDictionary`2,System.Collections.Generic.IDictionary`2) [0x00016] in d:\Downloads\Xamarin.Auth-master\Xamarin.Auth-master\src\Xamarin.Auth\OAuth2Authenticator.cs:282
at Xamarin.Auth.WebRedirectAuthenticator.OnPageEncoun...[intentionally cut off]

I'm struggling with this issue for a while now. Please help!

valdetero
  • 4,624
  • 1
  • 31
  • 46
HansElsen
  • 1,639
  • 5
  • 27
  • 47
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Lasse V. Karlsen May 26 '15 at 02:38

1 Answers1

0

I've found it! It was a mix of circumstances.
My debugger didn't stop at the breakpoints (don't know why).
What caused the problem was that I created an Object with the Login method above in an OnCreate() method.
Then I attached an EventHandler to an event of that Object.
The moment the Authenticator comes back from his Intent, the Context where my object was bound is gone.
It maybe is a bit vague to understand but maybe some code will clarify further.

//Not working, causes the problem
public class MyActivity {
    MyAuthenticator auth; //the object containing Login();
    public void OnCreate() {
        auth=new MyAuthenticator();
        auth.LoggedIn += blabla;
    }
    public void SomeMethod() {
        auth.Login();
    }
}

Solution:

//Working, own scope
public class MyActivity {
    public void OnCreate() {
        //ILB
    }
    public void SomeMethod() {
        var auth=new MyAuthenticator();
        auth.LoggedIn += blabla;
        auth.Login();
    }
}
valdetero
  • 4,624
  • 1
  • 31
  • 46
HansElsen
  • 1,639
  • 5
  • 27
  • 47