"Object reference not set" when loading a textbox in a user control

Discussion in 'ASP .Net Building Controls' started by Monty, Mar 29, 2006.

  1. Monty

    Monty Guest

    Hi All,

    I have a user control that shows name and address for a person in a text
    box. When the page hosting the control loads up, it calls a method on the
    user control that loads up the textbox fields. Occasionally, but not all the
    time, I will get an error when I try to set the value of one of the
    controls. The error message is "Object reference not set to an instance of
    an object". I used to get this (I believe more frequently but am not sure)
    when I called the user control's method from the page's Load event, so I
    moved it to the page's PreRender event, but I still get the error every now
    and again. The line it's occurring on looks like this:

    txtAddress1.Value = Left$(sAddressBlock, iPos - 1)
    The textbox "txtAddress1" definitely exists and works > 90% of the time. Any
    thoughts? TIA!
    Monty, Mar 29, 2006
    1. Advertisements

  2. Monty

    sam Guest

    Is it the textbox that's null or the string sAddressBlock? And also,
    during what part of the user control's lifecycle are you adding the
    textbox to the user control?
    sam, Mar 30, 2006
    1. Advertisements

  3. Thanks for sam's input.

    Hi Monty,

    As sam has mentioned, it's important that we call the property(which use
    code to access the TextBox sub control) at the proper time during the
    Usercontrol's lifecycle. If we call it too earlier within the ASP.NET
    control's lifecycle, the control may haven't been created, thus it will
    result to "Null reference" error... I suggest you provide some further
    code logic or snippet on your usercontrol so that we can have a look into


    Steven Cheng
    Microsoft Online Community Support


    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    Steven Cheng[MSFT], Mar 30, 2006
  4. Monty

    Monty Guest

    Hi Sam,

    Good questions. In it's real branch of code sAddressBlock is not going to be
    null and ipos is always greater than zero. I made a tester in VB.Net to test
    all other scenarios, but at worst the value is an empty string, never "null"
    or "nothing". Here's the tester:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button2.Click
    Dim sAddressBlock As String = "part1" & vbCrLf & "part2" 'I
    also tried these scenarios as well:
    'Dim sAddressBlock As String = vbCrLf & "part2"
    'Dim sAddressBlock As String = "part1" & vbCrLf
    'Dim sAddressBlock As String = vbCrLf
    Dim ipos As Integer = InStr(sAddressBlock, vbCrLf)
    ipos - 1) Is Nothing)
    End Sub

    As for adding the conrol, the control has already been added declaratively
    in design time, like so:

    <td noWrap align="right" width="110">Address:</td>
    <td><input id="txtAddress1" type="text" name="txtAddress1"
    runat="server"><SPAN class="RequiredFieldMarker">*</SPAN>
    <asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server"
    ErrorMessage="Required Field" Display="Dynamic"

    Thanks for any light you can shed.
    Monty, Mar 30, 2006
  5. Monty

    sam Guest

    I'll just get back to you real quick on this.

    No, I can't see any reason why this error is happening. You are doing
    all the right things. If you missed a tag on the aspx or ascx page a
    parser error would be thrown. This kindof thing happens when you
    declare the textbox private or assign it a value of null in the code
    behind but you aren't doing that presumably. Question: Does the page
    load correctly first and then this comes up *after* that? Or does it
    happen just after a compile and then always happens? From your post I
    suspect the former, which means the framework is writing out the code
    correctly to add your textbox to the control tree (you can check this
    intermediate code - its somewhere in ASP.NET temporary files - google
    for more info).

    Anyway, I suspect a MS runtime error. Did you try restarting your
    computer? Does it happen when you are not debugging in visual studio?
    It could just be an issue with the way the VS debugger hooks into the
    running process. If thats true you won't have any issues in deployment
    and restarting your computer could fix your debugger for you.

    Interesting problem.
    sam, Mar 30, 2006
  6. Monty

    Monty Guest

    Thanks Sam, I can't see any reason either. It hasn't happened at all to me
    during debugging, only on the production machine. Again, the weird thing is
    that this only happens sporadically. I did ask them to restart the web
    server and now, coincidentally, they're having trouble reviving the
    machine... so if it doesn't come up I guess I won't have to worry about
    this! ;-)

    If I figure it out I'll let you know. Anyone else have any ideas?
    Monty, Mar 30, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.