0

I keep getting this error and i'm not sure how to fix it. Can anyone help me? (Starter with programming / Xaramin) PS: The mBtnSignUp.Click += .... code does work + shows the dialog, but the SignIn doesn't, while it's the same code + the code id is correct aswell.

Error: System.NullReferenceException: Object reference not set to an instance of an object.

Part of code thats giving me the error:

mBtnSignIn.Click += (object sender, EventArgs args) =>
        {
            //Dialog opvragen
            FragmentTransaction transaction = FragmentManager.BeginTransaction();
            dialog_SignIn signInDialog = new dialog_SignIn();
            signInDialog.Show(transaction, "Dialog fragment");

            signInDialog.mOnSignInComplete += SignInDialog_mOnSignInComplete;
        };

Full Code:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            mBtnSignUp = FindViewById<Button>(Resource.Id.btnRegistreren);
            mBtnSignIn = FindViewById<Button>(Resource.Id.btnInloggen);
            mProgressBar = FindViewById<ProgressBar>(Resource.Id.progressBar1);

            mBtnSignIn.Click += (object sender, EventArgs args) =>
            {
                //Dialog opvragen
                FragmentTransaction transaction = FragmentManager.BeginTransaction();
                dialog_SignIn signInDialog = new dialog_SignIn();
                signInDialog.Show(transaction, "Dialog fragment");

                signInDialog.mOnSignInComplete += SignInDialog_mOnSignInComplete;
            };

            mBtnSignUp.Click += (object sender, EventArgs args) =>
            {
                //Dialog opvragen
                FragmentTransaction transaction = FragmentManager.BeginTransaction();
                dialog_SignUp signUpDialog = new dialog_SignUp();
                signUpDialog.Show(transaction, "Dialog fragment");

                signUpDialog.mOnSignUpComplete += SignUpDialog_mOnSignUpComplete;
            };
        }

        void SignInDialog_mOnSignInComplete(object sender, OnSignInEventArgs e) //Inloggen
        {
            mProgressBar.Visibility = ViewStates.Invisible;
            Thread thread = new Thread(ActLikeARequest);
            thread.Start();
        }

        void SignUpDialog_mOnSignUpComplete(object sender, OnSignUpEventArgs e) //Registreren
        {

            mProgressBar.Visibility = ViewStates.Invisible;
            Thread thread = new Thread(ActLikeARequest);
            thread.Start();
        }

        private void ActLikeARequest()
        {
            Thread.Sleep(3000);

            RunOnUiThread(() => { mProgressBar.Visibility = ViewStates.Invisible; });
        }

I hope my question is understandable.

Thanks in advance!

guusapril
  • 33
  • 1
  • 1
  • 8
  • I know.. But because I haven't got much experience, I'm not really able to find the wrong line(s) in my code.. That's why I made this question – guusapril Feb 09 '18 at 07:55
  • Have you tried debugging your code while it's running? It would give you the best insight as where the exception is being thrown, which can lead you to fix your bug. – Paul Karam Feb 09 '18 at 07:57
  • I haven't tried that. Thanks for the tip! – guusapril Feb 09 '18 at 07:58

1 Answers1

0

You can tease the error and line of your code when an unhandled error happens by pressing continue and looking in the output window.

There usually is a lot of stuff going on here but if you look carefully it will give you clues and line numbers if you are running the emulator or using remote debugging for the device

The other way is to wrap the offending code in a try catch and break point the exception and look at the stack trace

try
{
    // code
}
catch(Exception ex)
{  // << break point here

}

Additionally in production you can use HockeyApp to monitor crashes

Additional Resources

Using Breakpoints

How to integrate HockeyApp with Xamarin

TheGeneral
  • 79,002
  • 9
  • 103
  • 141