[RESOLVED] NullReferenceException was unhandled (passing array of string)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] NullReferenceException was unhandled (passing array of string)

  1. #1
    Join Date
    May 2009
    Posts
    32

    [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.

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    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.

  3. #3
    Join Date
    May 2009
    Posts
    32

    Re: NullReferenceException was unhandled (passing array of string)

    Quote Originally Posted by CGKevin View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center