Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Monty - 29 Mar 2006 22:33 GMT
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!
sam - 30 Mar 2006 00:01 GMT
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?
Steven Cheng[MSFT] - 30 Mar 2006 09:01 GMT
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
it.

Regards,

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.

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Monty - 30 Mar 2006 15:00 GMT
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)
       Console.WriteLine(Microsoft.VisualBasic.Strings.Left$(sAddressBlock,
ipos - 1) Is Nothing)
   End Sub

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

<tr>
 <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"
ControlToValidate="txtAddress1"></asp:requiredfieldvalidator></td>
</tr>

Thanks for any light you can shed.

> 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 - 30 Mar 2006 19:28 GMT
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.
Monty - 30 Mar 2006 21:48 GMT
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?

> I'll just get back to you real quick on this.
>
[quoted text clipped - 17 lines]
>
> Interesting problem.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.