locked
Strange Occurance - NullReferenceException when value is entered RRS feed

  • Question

  • I have a textbox on a form that asks the user to input an ID number. I then take that integer and assign it to a property of a class array. When I do so, I get an error:

    "NullReferenceException was unhandled. Object reference not set to an instance of an object."

    I don't quite understand this because I know I am entering a value and converting it correctly.

    For troubleshooting, I added a label on my form to output the value in the textbox when I click a button. Sure enough, if I enter '5' (or any number) the label says nothing and I get the error. If I enter 'five' the label reads five and I get my invalid ID error that I created.

    I've compared this to my other programs and have never had this. Looks right to me. I'm sure it's stupidly simple I just can't get my head around it!

    CODE EXCERPT:

    public partial class MainForm : Form { Employee[] employeeArray = new Employee[3]; int x = -1, idNum; bool valid = false; public MainForm() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { x++; valid = int.TryParse(txtID.Text, out idNum);
    if (valid == false) { MessageBox.Show("Enter ID number", "ERROR", MessageBoxButtons.OK); x--; } else { employeeArray[x].IdNumber = idNum; // Error on idNum ...


    CLASS:

        class Employee
        {
            int idNum;
            double wage;
    
            public int IdNumber
            {
                set
                { idNum = value; }
                get
                { return idNum; }
            }
              
            public double HourlyWage 
            {
                set
                {
                    if (wage >= 6.00 || wage <= 50.00)
                        wage = value;
                }
                get
                { return wage; }
            }
        }




    • Edited by mallorz Thursday, March 7, 2013 2:26 AM
    Thursday, March 7, 2013 2:24 AM

Answers

  • Although you created an Array you never actually instantiated an instance of the employee object. In the else block do this. 

    else
    {
       employeeArray[x] = new Employee();
       employeeArray[x].IdNumber = idNum; // Error on idNum
    }


    This way, employeeArray[x] gets instantiated and the object is created.
    • Edited by Oasisfactor Thursday, March 7, 2013 2:32 AM
    • Proposed as answer by PaulLinton Thursday, March 7, 2013 2:51 AM
    • Marked as answer by mallorz Thursday, March 7, 2013 1:55 PM
    Thursday, March 7, 2013 2:31 AM

All replies

  • Although you created an Array you never actually instantiated an instance of the employee object. In the else block do this. 

    else
    {
       employeeArray[x] = new Employee();
       employeeArray[x].IdNumber = idNum; // Error on idNum
    }


    This way, employeeArray[x] gets instantiated and the object is created.
    • Edited by Oasisfactor Thursday, March 7, 2013 2:32 AM
    • Proposed as answer by PaulLinton Thursday, March 7, 2013 2:51 AM
    • Marked as answer by mallorz Thursday, March 7, 2013 1:55 PM
    Thursday, March 7, 2013 2:31 AM
  • I think it probably has to do with the employeeArray[x] ... although, I would think you'd get a different error of the x is out of bounds of the array.

    Easiest thing to do is debug this .... set a breakpoint in the Click and step through each line, taking a look at the values of objects as you go.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Thursday, March 7, 2013 2:33 AM
  • The array isn't out of bounds. employeeArray[x] is not set or not created, so it can't use it to set the idNum. Hence the error: NullReferenceException was unhandled. Object reference not set to an instance of an object
    Thursday, March 7, 2013 2:37 AM
  • Haha. Wow.  I don't have my laptop and program on me at the moment but you are correct, I never instantiated a new employee. Der.

    I guess I find the error a bit strange. You would think the label would still printout the integer.

    Thank you very much!

    • Edited by mallorz Thursday, March 7, 2013 2:12 PM
    Thursday, March 7, 2013 1:57 PM