为什么会引发空对象异常

时间:2013-01-21 21:35:19

标签: c# nullpointerexception

我得到的Object引用未设置为对象的实例。:OperationAssembly:0行157.我不明白什么可能触发此异常

LogFile.Info("order="+order.Id);

if (order.Location != null)
{
    LogFile.Info("order=" + order.Location.Address);
}    
else
{
    LogFile.Info("order location is null");
}

LogFile.Info("order=" + order.Location.Latitude);
LogFile.Info("order=" +  order.IsResolved);
LogFile.Info("order user id=" + order.User.Id);
LogFile.Info("order ser name=" + order.User.Name);
LogFile.Info("order ser dest =" + order.Destination.Address);                       

line 157:
result = new OrderDataDriver
{
    OrderId = order.Id,
    Address = order.Location.Address,
    Lat = order.Location.Latitude,
    Lng = order.Location.Longitude,
    DestinationAddress = order.Destination.Address,
    DestinationLat = order.Destination.Latitude,
    DestinationLng = order.Destination.Longitude,
    IsCanceled = order.IsCanceled,
    IsPendingResponse = order.IsPendingResponse,
    IsResolved = order.IsResolved,
    UserId = order.User.Id,
    Message = order.MessageForDriver,
    UserName = order.User.Name,
    // IsAdvanceBooking = order.AdvenceBookingTime != null,
    AdvanceBookingTime = order.AdvenceBookingTime,
    AdvancePrice = order.CalculatedPrice,
    AdvanceDistance = order.CalculatedDistance,
    SecondsToRespond = 30,
    Status=order.DriverStatus
};

[DataContract]
public class OrderDataDriver
{

    [DataMember] public string Address { get; set; }
    [DataMember] public string Feedback { get; set; }
    [DataMember] public double Lat { get; set; }
    [DataMember] public double Lng { get; set; }
    [DataMember] public int UserId { get; set; }
    [DataMember] public string Status { get; set; }
    [DataMember] public bool? IsEligible { get; set; }
    [DataMember] public bool? IsCanceled { get; set; }
    [DataMember] public bool? IsResolved { get; set; }
    [DataMember] public bool? IsPendingResponse { get; set; }
    [DataMember] public bool? AllowsTracking { get; set; }
    [DataMember]
    public int OrderId { get; set; }
    [DataMember]


    public int App { get; set; }
    [DataMember]
    public int? PreferedDriver { get; set; }
    [DataMember]
    public bool IsAdvanceBooking { get; set; }
    [DataMember]
    public DateTime? AdvanceBookingTime { get; set; }
    [DataMember]
    public double? AdvancePrice { get; set; }
    [DataMember]
    public double? AdvanceDistance { get; set; }
    [DataMember]
    public string DestinationAddress { get; set; }
    [DataMember]
    public double DestinationLat { get; set; }
    [DataMember]
    public double DestinationLng { get; set; }
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public int SecondsToRespond { get; set; }
}

4 个答案:

答案 0 :(得分:4)

将复杂对象初始化更改为以下内容:

var result = new OrderDataDriver();
result.blablah = foor.bar;

然后,您将收到一条更好的错误消息,其中包含您尝试访问空对象属性的确切行号。

这是一个参考这个可爱模式的好地方,这使得开发人员的生活更轻松 http://en.wikipedia.org/wiki/Fail-fast

答案 1 :(得分:0)

如果属性为null,则这些访问器中的任何一个都会爆炸:

//If Location is null BOOM!!!
Lat = order.Location.Latitude,
Lng = order.Location.Longitude,

//If Destination is null BOOM!!!
DestinationAddress = order.Destination.Address,
DestinationLat = order.Destination.Latitude,
DestinationLng = order.Destination.Longitude,

答案 2 :(得分:0)

由于已经记录了可能null的内容,因此它们应该早先被捕获。

除非你有一个带有副作用的属性吸气剂。那些是纯粹的邪恶,所以要小心。另一种可能是另一个线程改变了一个值。这更难以证明。

答案 3 :(得分:0)

使用对象初始值设定项时,例如

result = new OrderDataDriver { OrderId = order.Id, Address = order.Location.Address, Lat = order.Location.Latitude, Lng = order.Location.Longitude, };

不要指望行号(和列号,如果有的话)指出大括号{ }内的哪些“属性赋值”导致异常。毕竟这被转换为临时(不可见)变量属性的赋值。只有一切顺利,临时变量才会复制到result值。

我也遇到了涉及对象初始化器的堆栈跟踪的不便。