Saturday, March 24, 2012

object reference not set to an instance of an object

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?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();");
}

"KenLee" <KenLee@.discussions.microsoft.com> wrote in message
news:FB958839-225C-41AE-8ABF-8FC5C1D81746@.microsoft.com...
> 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?
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

"KenLee" wrote:

> 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?

0 comments:

Post a Comment