object reference not set to an instance of an object

G

Guest

Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
TextBox)
Override.Attributes.Add("onblur", "Override();")

why do I get the message like "object reference not set to an instance of an
object"?
how can I fix?
 
S

sloan

It doesn't find the control.

You should 99.99 % of the time check for null (Nothing in vb.net) when you
code.

Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
TextBox)

if (Not (Override Is Nothing)) Then

Override.Attributes.Add("onblur", "Override();")
End If


Or better yet:

if (Not (Override Is Nothing)) Then
Override.Attributes.Add("onblur", "Override();")
else
Throw New ArgumentException("Excected Control was not found")
End If


...

C#

TextBox tb = uxTimeEntry.FindControl("uxOverride") as TextBox;
//or
//TextBox tb = (TextBox)uxTimeEntry.FindControl("uxOverride") ;
if (null!=tb)
{
tb.Attributes.Add("onblur", "Override();");
}
 
G

Guest

Kenly
findcontrol returned nothing or uxtimeentry is nothing. Check to make sure
your passing in the correct controlid it might be something like uxOverride1
or the case could be incorrect. Run in debug and display
uxTimeEntry.controls one by one and try the ctype in the immediate window.

Good Luck
DWS
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top