Saturday, March 24, 2012

Object reference not set to an instance of an object

I created classes, which should collect information about user, but when I am referencing that class its give me an error:
Here is my class:

PublicClass clientConnectInfo
Inherits System.Web.UI.UserControl
PublicFunction getIpAddress()AsString
Return Request.ServerVariables("REMOTE_ADDR")
EndFunction
EndClass
That how I am referencing:

Dim clientInfoAsNew clientConnectInfo
Dim ipAddressAsString = clientInfo.getIpAddress
Error:

Object reference not set to an instance of an object.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:

Line 8: Line 9: Public Function getIpAddress() As StringLine 10: Return Request.ServerVariables("REMOTE_ADDR")Line 11: End FunctionLine 12:

What am I doing wrong? Please Help!Try:
Return HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")

It workded! Thank you very much!

0 comments:

Post a Comment