3 Replies - 2682 Views - Last Post: 07 July 2011 - 07:06 AM Rate Topic: -----

#1 dtost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-July 11

Object reference not set to an instance of an object

Posted 07 July 2011 - 05:29 AM

I am having trouble with this error and I believe it is because of how I am trying to create my array.
What I am trying to do is create a list of dates I convert the date to a string then want to assign those dates to my array.


Label[] lblDay = new Label[13];
for (int i = 0; i != (lblDay.Length); i++)
  {
     payPeriodRange = dateEOFPayPeriod - TimeSpan.FromDays(i); //Calculate the Pay Period Range
     lbltimesheetDay = payPeriodRange.ToString(dateformat);
     lblDay[i].Text = lbltimesheetDay;
  }




I get "Object reference not set to an instance of an object" at the last line
lblDay[i].Text = lbltimesheetDay;

What am I missing?

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Object reference not set to an instance of an object

Posted 07 July 2011 - 05:33 AM

lblDay accepts items of type Label : Label being a class, every item inside that array is an object. An object has to be instantiated before you can use it. Before the line with the error, add
Label newLabel = new Label();
newLabel.Text = your_value;
tblLabel[i] = newLabel;


Was This Post Helpful? 1
  • +
  • -

#3 dtost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-July 11

Re: Object reference not set to an instance of an object

Posted 07 July 2011 - 06:21 AM

Thanks!
That solved my problem but it seems as if what I was trying to do is not working.

lblDay[i].Text = my_value;
was suppose to populate my label on a form which was label like the following:

<asp:Label ID="lblDay0" runat="server" Text=""></asp:Label>
<asp:Label ID="lblDay1" runat="server" Text=""></asp:Label>
etc....



But none of the values are appearing
I was thinking that ldlDay0 would be the same as lblDay[i] but it doesn't appear to like that.
Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object

Posted 07 July 2011 - 07:06 AM

Just because you make an array and reserve space for 13 elements doesn't automatically populate the array.

Label[] lblDay = new Label[13];



This just saves the space.
YOu still need to make all 13 elements something other than null. IN other words you have to assign a new label to each element. A loop is good for this.

for(Index a = 0; Index < lblDay.Length; Index++)
{
  lblDay[Index] = new Label();
}


I'd bet the new values are appearing.
I don't see anyplace where you have set the .Location of the labels. So they are all at their default, right on top of each other. All you're going to see is the top one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1