I have a class called LoginDetails which contains usernames and passwords, along with their accessor and mutator functions.
In my webform (frmLogin) I just have 1 button called btnLogin. When I press the button I want the username to display. However, when I run the program it is saying that the element value is null which makes me think the problem is the element is not being initialised. Unfortunately I cannot see what I am doing wrong here.
namespace Coffee_Shop_Login
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
//*********************************************************************************************
//*********************************************************************************************
//Global Variables
staticint 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
privatevoid 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()
//*********************************************************************************************
//*********************************************************************************************
privatevoid 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
//========================
publicvoid Set_Employee_Username(string username)
{
Employee_Username = username;//sets Employee_Username with value passed in
}//end of Set_Employee_Username()
//*********************************************************************************************
//*********************************************************************************************
publicvoid 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;
//*********************************************************************************************
//****************************************** THE END ******************************************
//*********************************************************************************************
}
}
The error message I get when trying to compile is:
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.
I have tried removing the array from the object and it works so it doesn't appear to be anything wrong with the class itself.
I have also changed the code in the load event to show a message box when the form loads but the message is not displaying. This make me think that there is something wrong with this line:
I can't reproduce the error from the information given.
Edit:
Missed this:
I have also changed the code in the load event to show a message box when the form loads but the message is not displaying.
How did you add the event handler? i.e. privatevoid frmLogin_Load(object sender, EventArgs e)
If you just added the function by typing it then you will need to 'wire it in'.