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?