1

I am trying to initialise the elements of my array (which is an array of the class) in a load event but it doesn't get called and can't see what I am doing wrong.

This is my code:

namespace Coffee_Shop_Login
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
    
        //Global Variables
        static int Maximum_Number_Of_Logins = 1;
        LoginDetails[] Employees = new LoginDetails[Maximum_Number_Of_Logins];//creating array of objects of type LoginDetails


        //method initialises the array objects when form loads
        private void frmLogin_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Employees.Length; i++)
            {
                Employees[i] = new LoginDetails(); // set up single element to a new instance of the object
            }
        }//end of frmLogin_Load() 

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = "Johnny";
            Employees[0].Set_Employee_Username(username);
            MessageBox.Show("Username is: " + Employees[0].Get_Employee_Username());
        }
    }
}

namespace Coffee_Shop_Login
{
    class LoginDetails
    {
        //Public Mutator Functions
        //========================

        public void Set_Employee_Username(string username)
        {
            Employee_Username = username;//sets Employee_Username with value passed in
        }//end of Set_Employee_Username()


        public void Set_Employee_Password(string password)
        {
            Employee_Password = password;//sets Employee_Password with value passed in
        }//end of Set_Employee_Password()        


        //Public Accessor Functions
        //=========================

        public string Get_Employee_Username()
        {
            return Employee_Username;//returns the value of Employee_Username
        }//end of Get_Employee_Username()


        public string Get_Employee_Password()
        {
            return Employee_Password;//returns the value of Employee_Password
        }//end of Get_Employee_Password()


        //Private Member Variables
        private string Employee_Username;
        private string Employee_Password;

    }
}

When I run the application I get this error message due to the load event not being called:

An unhandled exception of type 'System.NullReferenceException' occurred in Coffee Shop Login.exe

Additional information: Object reference not set to an instance of an object.

What do I need to add to make the Load event call my method?

Community
  • 1
  • 1
  • Can you tell me which line you are getting the error? Other than the poor style of writing Java like code in C#, I don't find any reason why your code doesn't work as it runs fine for me. Error may be somewhere else. With that said, seriously consider learning C# and its basics first like properties and related conventions. – Mat J Mar 30 '14 at 12:57
  • Only thing I can see is that the method `frmLogin_Load` is not registered with the event and it creates a NRE when clicking on the button – Alexandre Pepin Mar 30 '14 at 13:05
  • Alexandre, I agree that the problem is with my load event but I believe I have followed the proper format and have the name correct, so can't understand why it won't run. – user3478049 Mar 30 '14 at 13:21

1 Answers1

0

Try to instantiate your array in the form constructor instead of nowhere in the class, like this:

public frmLogin()
    {
        InitializeComponent();
        Employees = new LoginDetails[Maximum_Number_Of_Logins];
    }
Karim AG
  • 2,097
  • 13
  • 28
  • Thanks for that Karim, it is now working. However, I don't understand why my load event would not execute. – user3478049 Mar 30 '14 at 13:16
  • The language specification forbids the execution of arbitrary statements at the class level.. refer to this [**answer**](http://stackoverflow.com/questions/13198167/why-cant-i-set-the-property-value-of-a-class-object-without-being-in-a-method) for more information – Karim AG Mar 30 '14 at 13:54