0

I was just trying to make a simple app with Xamarin.android from the tutorial shown https://learn.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;
        }
    }
4
  • 1
    You need to debug more, what line is it happening on Commented Jul 11, 2019 at 7: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 Commented Jul 11, 2019 at 7:36
  • debugging stops automatically at translateButton.Clicked += OnTranslate; Commented Jul 11, 2019 at 7: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 first. Commented Jul 11, 2019 at 8:12

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.