i keep getting this error message: Object reference not set to an instance of an object.
private static void GetIPInfo(User user)
{
string ipAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
string city = string.Empty;
string region = string.Empty;
string country = string.Empty;
double? latitude = -1.00;
double? longitude = -1.00;
LocationTools.GetLocationFromIP(ipAddr开发者_如何学Goess, out city, out region, out country, out latitude, out longitude);
user.IPAddress = user.IPAddress; **//error is pointing here**
}
Do i need to instantiate something?
Would it be something like this to solve the problem?
user.IPAddress new user.IPAddress = user.ipAddress;
What exactly did you expect this statement to achieve:
user.IPAddress = user.IPAddress;
I'd expect it to be at best a no-op. As it happens, it seems that the value for the user
variable is null.
It's not clear what the method is meant to do, but presumably if user
is null then the request is unauthenticated. It's odd that a GetXyz
method has a void
return type. Should it be PopulateIPInfo
for example? In that situation it sounds like it would be a bug to be passed a null reference - but you really need to decide what the method is meant to do... It's unlikely that creating a new User
instance inside that method is going to be useful in the long run.
It depends on the way you are calling GetIPInfo(User user). It seems that you are passin null as user. Maybe you should write something like
GetIPInfo(new User());
But it is not clear whether User has to be initialized some way, or it is enough to create an empty instance. Furthermore, I do not understand what you are trying to do with user.IPAddress = user.IPAddress; It is an instruction that does not seem to have any effect at all, unless there is some other code in the setter of IPAddress which causes some collateral effect, but this is something I would avoid.
EDIT after your comment:
If I got it right (but I'm not really sure), maybe this is more similar to what you really need:
private static User GetIPInfo() { User user = new User();
string ipAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
string city = string.Empty;
string region = string.Empty;
string country = string.Empty;
double? latitude = -1.00;
double? longitude = -1.00;
LocationTools.GetLocationFromIP(ipAddress, out city, out region, out country, out latitude, out longitude);
user.IPAddress = ipAddress;
// other code to fill the other fields of User
return user;
}
Then you can call it and get a new instance of User that you can assign to a variable or use as you like.
How about:
user = new User; // Or whatever user is supposed to be
user.IPAddress = ipAddress; // Now you can assign an ip address to this property
As Jon pointed out, your user
seems to be null. I would think the problem is in the calling method, not in GetIPInfo
. You might want to add some argument checking to your methods. Also, I think that this method looks odd, but I might just not have the proper context to understand why it looks like it does.
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
throws System.NullReferenceException
– “Object reference not set to an
instance of an object.”
-error if IP address is not provided in the request. So you should check if the statement throws an exception.
精彩评论