Skip to content

Object reference not set to an instance of an object.

Jonathen Scalet

Asked Mar 28, 2006, 5:42 PM EST3 answers
Share
LinkedInX

The following code throws this error:

An unhandled exception of type 'System.NullReferenceException' occurred in Classes.exe
Additional information: Object reference not set to an instance of an object.

When I call the form load function it throws the above error and Im not sure why.

C#
namespace Classes
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class ActSessions
{
public ActSessions()
{
//
// TODO: Add constructor logic here
//
}
public class User
{
public string name;
public string tabindex;
}
public User [] UserArray = new User[100];
}
}


private void Form1_Load(object sender, System.EventArgs e)
{
ActSessions Active = new ActSessions();
Active.UserArray[0].name = "JOHN"; //this line throw the error
}

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    "// TODO: Add constructor logic here" <--- Do that... you need to fill the array with blank user objects. There is no elements in the array and you are trying to set element zero's name to something.... well there is no element zero!!!
    0
  • 0
    Sorry I am a C# Sharp noob, how would I create those User objects?

    Thanks,
    Jon
    0
  • 0
    I made an example for you: http://rafb.net/paste/results/M0TtEw85.html
    0