My IsNullOrEmtpy is not responding giving me a crash every time I leave my edittext empty, Help?

I use Xamarin android where I want to alert a user if he forgets to enter his code to login. Currently the only thing that seems to be responding is my app crashing.. No error or warning is giving me information where I'm going wrong, so what am I not seeing?

void OnAanmeldClick(object sender, EventArgs e)
{
int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);

        EditText codeinput = FindViewById<EditText>(Resource.Id.codeInput);

        if (string.IsNullOrEmpty(codeinput.Text))
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.SetTitle("ERROR");
            alertDialog.SetMessage("Vul wel wat in");
            alertDialog.SetNeutralButton("OK", delegate
            {
                alertDialog.Dispose();
            });
            alertDialog.Show();
        }

Best Answer

  • jezhjezh Xamurai
    Accepted Answer

    You could recheck the code:

      int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);
    

    Since we have use the following code :

             EditText codeinput = FindViewById<EditText>(Resource.Id.codeInput);
    

    Why are you use the code

       int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);
    

    When we not enter the input or enter a string which can't convert to int, then the code ' int.Parse()' will crash.
    So you could try to delete the code or comment out this line of code.

    // int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @Derpsider said:
    Currently the only thing that seems to be responding is my app crashing.. No error or warning is giving me information where I'm going wrong, so what am I not seeing?

    A "crash" is the result of an unhandled exception. If you use VS to debug your app, you should see details of the exception thrown, either in the Output pane or elsewhere.

    You should also be able to step line-by-line through your OnAanmeldClick method to see which line is throwing the exception.

    There are various possibilities, starting with FindViewById<EditText>(Resource.Id.codeInput) returning null. If that happens, your code will produce a null reference exception.

    Once you have captured the exception details and identified the line the exception occurs on, if you still have problems, post that information here.

    Of course, once you have identified and resolved the immediate problem, you may want to add exception handling to prevent future exceptions from making your app exit. Similarly, you may want to integrate with App Center or similar to report exceptions back to you when they occur, whether in development or production.

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You could recheck the code:

      int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);
    

    Since we have use the following code :

             EditText codeinput = FindViewById<EditText>(Resource.Id.codeInput);
    

    Why are you use the code

       int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);
    

    When we not enter the input or enter a string which can't convert to int, then the code ' int.Parse()' will crash.
    So you could try to delete the code or comment out this line of code.

    // int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);

  • @jezh I was using int code = int.Parse(FindViewById(Resource.Id.codeInput).Text);

    if (code == 1118480 || code == 1101781 || code == 1114579) {
    // next action
    }

    due to if (codeinput = 'barriercode' || codeinput = 'barriercode')
    giving me an error respond back.

    I've changed now fixed the problem by changing my int code to a string.
    string code = FindViewById(Resource.Id.codeInput).Text;

    Also thank you for your response @JohnHardman, I forgot I also could've just a try catch to stop the crashing. I'll surely be using that in my application now.

Sign In or Register to comment.