Reputation: 5546
I get a Object reference not set to an instance of an object.:OperatorAssembly: 0 line 157. I don't understand what could trigger this exception
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; }
}
Upvotes: 2
Views: 127
Reputation: 61952
When you use an object initializer, like
result = new OrderDataDriver { OrderId = order.Id, Address = order.Location.Address, Lat = order.Location.Latitude, Lng = order.Location.Longitude, };
don't expect the line number (and column number if you have that) to point out exactly which of the "property assignments" inside the braces { }
leads to an exception. After all this is translated to assignments to properties of a temporary (invisible) variable. Only if all goes well, the temporary variable is copied to your result
value.
I have also experienced this inconvenience with the stack trace involving object initializers.
Upvotes: 0
Reputation: 283664
Since things that could be null
have already been logged, they should have been caught earlier.
Unless you have a property getter with side effects. Those are pure evil, so watch out. Another possibility is that another thread changed a value. That's even harder to prove.
Upvotes: 0
Reputation: 14874
change the complex object initialization to something like this:
var result = new OrderDataDriver();
result.blablah = foor.bar;
Then you will get a better error message which contains the exact line number which you tried to access a property of a null object.
Here is a good place to refere to this lovely pattern which makes the life of developers easier http://en.wikipedia.org/wiki/Fail-fast
Upvotes: 4
Reputation: 44906
Any one of these accessors could blow up if the property is 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,
Upvotes: 0