0

Here's code I have written for a simple event based adder, this is the error I constantly get: Object reference not set to an instance of an object.

I am a beginner, so what's my error here? What am I missing? Thanks.

private void txtTwo_TextChanged(object sender, TextChangedEventArgs e)
{
            int numberOne, numberTwo, number3;
            if (int.TryParse(txtOne.Text, out numberOne))
            {
                // DO NOTHING
            }
            else
            {
                MessageBoxButton buttons = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Error;
                MessageBox.Show("Not An Integer! Only Integers Allowed !", "Error : First Number", buttons, icon);
                txtOne.Clear();
            }
            if (int.TryParse(txtTwo.Text, out numberTwo))
            {
                //DO NOTHING
            }
            else
            {
                MessageBoxButton buttons2 = MessageBoxButton.OK;
                MessageBoxImage icon2 = MessageBoxImage.Error;
                MessageBox.Show("Not An Integer! Only Integers Allowed !", "Error : Second Number", buttons2, icon2);
                txtTwo.Clear();
            }

            number3 = numberOne + numberTwo;
            string num3 = number3.ToString();
            txtOut.Text = num3;
}
17
  • 3
    Where exactly are you getting the exception? Commented May 1, 2013 at 9:55
  • 1
    last line : txtOut.Text = num3; And I am pretty puzzled why it throws the exception. Commented May 1, 2013 at 9:58
  • 1
    I think num3 cannot be null since number3 will always have a default non null value. More like txtOut is not instantiated. Is txOut visible on the Windows Form? Commented May 1, 2013 at 10:00
  • 2
    Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented May 1, 2013 at 10:02
  • 2
    num3 cannot be null Commented May 1, 2013 at 10:19

2 Answers 2

3

In this case it must be txtOut that is null, because num3 has been initialised. Try renaming your txtOut control in the designer or deleting and and recreating it.

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

Comments

0

So finally got it figured.

Well, num3 can't be null because it's a value type, that means that txt3 is null. txtOut is null because the TextChanged event is raised when txtTwo's Text property is set in XAML which likely happens before the txtOut TextBox is created.

So the solution is to remove the TextChanged event from XAML and put it in the constructor, after InitializeComponent:

public MainWindow() {
    InitializeComponent();
    txtTwo.TextChanged += txtTwo_TextChanged;
}

Comments

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.