Visual C# - Problem Initialising Array Elements

Hi,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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());
        }
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;

        //*********************************************************************************************
        //****************************************** 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.
Last edited on
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:

 
private void frmLogin_Load(object sender, EventArgs e)

Please could someone help me with this.

Thanks
closed account (z05DSL3A)
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. private void frmLogin_Load(object sender, EventArgs e)
If you just added the function by typing it then you will need to 'wire it in'.
Last edited on by Canis lupus
This is a C++ forum.
closed account (z05DSL3A)
RabMac, If you want to keep the likes of mutexe happy, ask your C# questions in the lounge. ;0)
:)
I'm neither happy or unhappy, i just think maybe the OP would get more help help and faster help if he/she posted on a C# forum.
Topic archived. No new replies allowed.