2

I've created a class

public partial class Subrank
{
    private System.DateTime startDateField;

    public System.DateTime StartDate
    {
        get
        {
            return this.startDateField;
        }
        set
        {
            this.startDateField = value;
        }
    }
}

im then trying to create an array of this...

Subrank[] pastSubRank = new Subrank[1];
pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");

but pastSubRank[0] is crashing saying it is NULL....why is this?

John
  • 3,853
  • 20
  • 69
  • 146

3 Answers3

6

Yes cause you haven't instantiated the class Subrank before accessing it's property.

Subrank[] pastSubRank = new Subrank[1];
pastSubRank[0] = new Subrank();
pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");
Rahul
  • 73,987
  • 13
  • 62
  • 116
4

You need to create an object to put in the array before accessing property

        Subrank[] pastSubRank = new Subrank[1];
        pastSubRank[0] = new Subrank();
        pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");
Tallon
  • 41
  • 4
2
Subrank[] pastSubRank = new Subrank[]
{
    new Subrank() { StartDate = DateTime.Parse("2012-05-22") }
};
Rawitas Krungkaew
  • 5,944
  • 1
  • 15
  • 28