简体   繁体   English

未将对象引用设置为对象的实例,null

[英]Object reference not set to an instance of an object, null

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(ipAddress, 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. 碰巧的是, user变量的值似乎为空。

It's not clear what the method is meant to do, but presumably if user is null then the request is unauthenticated. 目前尚不清楚该方法的目的,但大概是如果user为null,则该请求将未经身份验证。 It's odd that a GetXyz method has a void return type. GetXyz方法具有void返回类型很奇怪。 Should it be PopulateIPInfo for example? 例如应该是PopulateIPInfo吗? 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. 在那种情况下,传递一个null引用似乎是一个错误-但您确实需要确定该方法的用途... 在该方法内创建一个新的User实例不太可能会有用从长远来看。

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

It depends on the way you are calling GetIPInfo(User user) . 这取决于您调用GetIPInfo(User user)的方式 It seems that you are passin null as user . 看来您以user身份传递null。 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. 但是尚不清楚是否必须以某种方式初始化User,或者是否足以创建一个空实例。 Furthermore, I do not understand what you are trying to do with user.IPAddress = user.IPAddress; 此外,我不了解您要使用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. 除非在IPAddress的设置器中有其他代码引起某种附带影响,否则这条指令似乎根本没有任何作用,但这是我要避免的事情。

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(); 私人静态用户GetIPInfo(){用户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. 然后,您可以调用它并获得User的新实例,该实例可以分配给变量或根据需要使用。

As Jon pointed out, your user seems to be null. 正如乔恩指出的那样,您的user似乎为空。 I would think the problem is in the calling method, not in GetIPInfo . 我认为问题出在调用方法中,而不是在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. System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]引发System.NullReferenceException“Object reference not set to an instance of an object.” -如果请求中未提供IP地址,则错误。 So you should check if the statement throws an exception. 因此,您应该检查该语句是否引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空引用:对象引用未设置为对象的实例 - Null Reference : Object reference not set to an instance of an object 对象引用未设置为检查null的对象实例 - object reference not set to an instance of an object in check for null “对象引用未设置为对象的实例”-但是没有什么是空的? - “Object reference not set to an instance of an object” - but nothing is null? 对象引用未设置为对象的实例(null DataSet ??) - Object reference not set to an instance of an object (null DataSet??) 未将对象引用设置为对象的实例,参数为null - Object reference not set to an instance of an object, parameter is null 获取对象引用未设置为对象实例的空引用对象的类型 - Get type of null reference object for Object reference not set to an instance of an object 对象引用未设置为实例而不为空 - Object reference not set to an instance while not being null 数据存储库实例Null-NullReferenceException吗? 你调用的对象是空的 - Data Repository instance Null - NullReferenceException? Object reference not set to an instance of an object 对象引用未设置为对象的实例-空引用异常 - Object reference not set to an instance of an object - Null Reference Exception 你调用的对象是空的。 与非null对象。 - Object reference not set to an instance of an object. with a non null object.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM