0

I was just trying to make a simple app with Xamarin.android from the tutorial shown https://docs.microsoft.com/en-us/learn/modules/create-a-mobile-app-with-xamarin-forms/5-exercise-create-phone-number-translator-app But when I build it, application crashes. I tried debugging and got an error in OnTranslate method as System.NullReferenceException: Object reference not set to an instance of an object.

I tried setting values of declared variables as null. Tried using null operator.

This is my code

    {
        Entry phoneNumberText = null;
        Button translateButton = null;
        Button callButton = null;
        string translatedNumber;
        public MainPage()
        {
           /*
               Some design code
           */
            translateButton.Clicked += OnTranslate;
            this.Content = panel;
        }

         void OnTranslate(object sender, EventArgs e)
        {
            string enterednum = phoneNumberText.Text;
            translatedNumber = Core.PhonewordTranslator.ToNumber(enterednum);

            if (!string.IsNullOrEmpty(translatedNumber))
            {
                callButton.IsEnabled = true;
                callButton.Text = "Call" + translatedNumber;
            }
            else
            {
                callButton.IsEnabled = false;
                callButton.Text = "Call";
            }
        }

I have set the values for entry and those two buttons in my design code. And this is the method to translate string to number

        {
            if (string.IsNullOrWhiteSpace(raw))
                return null;
            raw = raw?.ToUpperInvariant();
            var newNumber = new StringBuilder();

            foreach (var c in raw)
            {
                if (" -0123456789".Contains(c))
                    newNumber?.Append(c);
                else
                {
                    var result = TranslateToNumber(c);
                    if (result != null)
                        newNumber.Append(result);
                    //bad string?
                    else return null;
                }
            }
            return newNumber?.ToString();
        }

        static bool Contains(this string keystring, char c){
            return keystring?.IndexOf(c) >= 0;
            }

        static readonly string[] digits =
        {
            "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"
        };
        static int? TranslateToNumber(char c)
        {
            for(int i = 0; i<digits.Length; i++)
            {
                if (digits[i].Contains(c))
                    return 2 + i;
            }
            return null;
        }
    }
Vikesh Patil
  • 11
  • 2
  • 5
  • 1
    You need to debug more, what line is it happening on – TheGeneral Jul 11 '19 at 07:36
  • 1
    the stack trace of the exception object should tell you which specific line caused the exception, or you can use the debugger to step through the code until you identify the cause. This is basic c# debugging – Jason Jul 11 '19 at 07:36
  • debugging stops automatically at `translateButton.Clicked += OnTranslate;` – Vikesh Patil Jul 11 '19 at 07:51
  • This doesn't depend on Xamarin, you're quite clearly calling something on a `null` object. You assigned `null` to it instead of an instance, in the first place. Consider having a look at the [basics](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) first. – StackLloyd Jul 11 '19 at 08:12

1 Answers1

2

debugging stops automatically at translateButton.Clicked += OnTranslate;

that should tell you something. Here you're declaring a button but never instantiating it

Button translateButton = null;

so later when you attempt to assign an event handler, your button is still null, which causes the exception

translateButton.Clicked += OnTranslate;

you need to instantiate the button first

translateButton = new Button();
Jason
  • 73,476
  • 14
  • 119
  • 139
  • Thanks this solved my `NullReferenceException` but my method still not working when I clicked on that button. I will look what I can do. Thank you for your time. – Vikesh Patil Jul 11 '19 at 08:52