0

I tired doing a simple A+B addition program using xamarin but every time error system.NullReferenceException occurs for Button click.

Button button = FindViewById<Button>(Resource.Id.button1);
        button.Click += delegate {
            EditText number1 = FindViewById<EditText>(Resource.Id.editText1);
            EditText number2 = FindViewById<EditText>(Resource.Id.editText2);
            TextView res = FindViewById<TextView>(Resource.Id.textView1);
            int result = Convert.ToInt32(number1.Text) + Convert.ToInt32(number2.Text);
            res.Text = result.ToString();
        };

enter image description here

1
  • 1
    You have not set the activity content SetContentView (Resource.Layout.Main) Commented Nov 10, 2016 at 10:21

1 Answer 1

2

You commented out the line:

SetContentView(Resource.Layout.Main);

This "binds" the current activity to the content view. If you dont bind that your program cant find any elements like your button. So it gives you a object reference error.

So remove the // infront of the SetContentView and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi sorry Your solution worked and restart did it :) thanks a lot

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.