-
October 16th, 2012, 01:16 PM
#1
[RESOLVED] NullReferenceException was unhandled (passing array of string)
Greetings.
I'm getting the unhandled NullReferenceException error when I run the following code:
private void btnCalcMoonPhase_Click(object sender, EventArgs e)
{
MoonForm MF = new MoonForm();
DateTime dDate = Convert.ToDateTime(txtStartDate.Text);
string[] dtMoonDates = new string[10];
dDate = dDate.AddHours(5);
MF.GetFullMoonDates(dDate, ref dtMoonDates);
for(int iCount=0;iCount<10;iCount++)
{
TextBox tb = (TextBox)this.Controls["txtFullMoonDates" + iCount.ToString()];
tb.Text = dtMoonDates[iCount];
}
}
I am passing an empty array of strings (dtMoonDates) to MF.GetFullMoonDates that fills this array with formatted date 'strings'.
I have 10 textboxes named txtFullMoonDates1 to txtFullMoonDates9 so that I can loop and fill them.
The last line of this code "tb.Text = dtMoonDates[iCount]; is generating the error.
The exception details is "Object reference not set to an instance of an object."
I think it is referring to the Textbox "tb".
I'm baffled.
-
October 16th, 2012, 02:24 PM
#2
Re: NullReferenceException was unhandled (passing array of string)
A few possibilities:
1. MF.GetFullMoonDates is returning a string array with one or more null elements
2. The actual control for (TextBox)this.Controls["txtFullMoonDates" + iCount.ToString()] is not found on the form.
Either tb or dtMoonDates[iCount] is null, you need to determine which one, then why.
-
October 16th, 2012, 03:38 PM
#3
Re: NullReferenceException was unhandled (passing array of string)
Originally Posted by CGKevin
A few possibilities:
1. MF.GetFullMoonDates is returning a string array with one or more null elements
2. The actual control for (TextBox)this.Controls["txtFullMoonDates" + iCount.ToString()] is not found on the form.
Either tb or dtMoonDates[iCount] is null, you need to determine which one, then why.
Turns out the TextBox name was the problem. "txtFullMoonDates" should be "txtFullMoonDate".
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|