Tracking down error occurring with "Request" object

  • Thread starter Thread starter Ben Amada
  • Start date Start date
B

Ben Amada

Hello! There are a couple of visitors (out of dozens) to this web page who
are causing the following error to occur:

"Object reference not set to an instance of an object."

I'm actually catching this error in a Try-Catch block. I've narrowed down
the possible offending code to the 3 lines below:

Request.Browser.Cookies
Request.UserAgent.ToString
Request.UserHostAddress.ToString

Is it possible for any of the 3 lines of code above to generate the error
listed above? For instance, is is possible that "Browser", "UserAgent" or
"UserHostAddress" would not be automatically instantiated in some cases?

Any help or ideas are appreciated!
Ben
 
Ben,

It's probably the string casts. You can't cast Nothing to a string and the
UserAgent and / or UserHostAddress may not contain any info.

I would check if they are instantiated before the string cast with an if
then like this:

If Not Request.UserAgent Is Nothing Then
Dim UserAgent As String = Request.UserAgent.ToString
End If

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S. Justin Gengo said:
It's probably the string casts. You can't cast Nothing to a string and the
UserAgent and / or UserHostAddress may not contain any info.

I would check if they are instantiated before the string cast with an if
then like this:

If Not Request.UserAgent Is Nothing Then
Dim UserAgent As String = Request.UserAgent.ToString
End If

Thanks for the reply! Testing to see if UserAgent / UserHostAddress is
Nothing or not seems like a good solution -- I'll make that change right
away. I'm curious as to what browser, security settings and/or conditions
lead to UserAgent or UserHostAddress not containing any data. I thought
they always contain data. Hmm...

Ben
 
Ben,

I'm not certain which settings / services cause it either. It is really just
the way that the header is sent out. Almost any program or service could be
modifying them. Norton Personal Firewall, AOL, are likely culprits. :-)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S. Justin Gengo said:
Ben,

I'm not certain which settings / services cause it either. It is really
just the way that the header is sent out. Almost any program or service
could be modifying them. Norton Personal Firewall, AOL, are likely
culprits. :-)

I'm actually logging to a DB the user's UserAgent and UserHostAddress.
After making the change you suggested, I no longer get errors and I just
discovered by looking at the DB that for this particular user, it's his
UserAgent data which is empty. Also Request.Browser.Cookies is returning a
False value for this user which means that his browser doesn't even support
cookies which I've never seen before. Usually the return value for Cookies
will be True -- whether they have configured their browser to accept cookies
or not is another issue.

Thanks,
Ben
 
Back
Top