In this event handler:
public static void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
DateTimeOffset timeStampUTC = args.Position.Coordinate.Timestamp.ToUniversalTime();
DateTimeOffset timeStampLocal = timeStampUTC.LocalDateTime;
DateTimeOffset dateTimeStampUTC = timeStampUTC.DateTime;
RecordLocation(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude,
args.Position.CivicAddress.City, args.Position.CivicAddress.State, dateTimeStampUTC, timeStampLocal);
}
…I’m getting a Null Reference Exception because args.Position.CivicAddress is null (the rest of the args passed to RecordLocation() are valid). I reckon sometimes Position will be null, and sometimes it won’t. What can I do to let the times when no City or State is found pass through unabated? I tried to make those strings in RecordLocation()‘s definition nullable, but that won’t compile.
Do I need to check CivicAddress for null and create an overloaded version of my RecordLocation() method, or is there another way to handle this?
You just need to check it. For example:
If you want to do lots of things with
args.Position, you quite possibly want one “outer” if statement – quite possibly with a local variable to simplify things: