0

I'm trying to retrieve lat long from google maps using address. But when it's null my program throws null reference exception. How can I avoid this? I've tried !Double.IsNaN(point.Latitude) and if (point.Latitude != null) but it anyway throws an exception

foreach (string line in File.ReadLines(@"path of txt file"))
            {

                var locationService = new GoogleLocationService();
                var point = locationService.GetLatLongFromAddress(line);

                if (!Double.IsNaN(point.Latitude) || !Double.IsNaN(point.Longitude))
                {
                    //do something
                }
                else
                {
                    var latitude = point.Latitude;
                    var longitude = point.Longitude;
                }


            }

point.Latitude is type of double. I'm using GoogleMaps.LocationServices

I'm checking addresses from txt file, so some google can't find some addresses in map and because they're being null

  • The `double` type is incapable of being null. It's a value type, not a nullable type. Almost certainly it is `point` that is `null` in your example, but there's no way to answer your question given the lack of a good [mcve]. Instead, you should review the marked duplicate to do some debugging to figure out why your `point` variable is set to `null` (i.e. the `GetLatLongFromAddress()` method is returning `null`). If after doing that work you still need help and can provide a good [mcve] reliably reproducing the problem, please post a new question. – Peter Duniho Dec 07 '15 at 06:44
  • @PeterDuniho I'm checking addresses from txt file, so some google can't find some addresses in map and because they're being null. I've edited question – Giorgi Pilishvili Dec 07 '15 at 06:45
  • @Micky As I mentioned I'm using GoogleMaps.LocationService. point is variable which returns lat long of requested address. When he can't find address it returns null on Latitude and Longitude. – Giorgi Pilishvili Dec 07 '15 at 06:47
  • If you know that the address is invalid and that `GetLatLongFromAddress()` returns a null value when that is the case, why aren't you just checking `point != null`? – Peter Duniho Dec 07 '15 at 06:47
  • _"When he can't find address it returns null on Latitude and Longitude"_ -- **no**, it does not. The `Latitude` and `Longitude` fields **cannot be null**. They have the type `double` (I checked the source code myself), which is a value type; value types cannot be null. As I said in my first comment, undoubtedly it is `point` that is null (i.e. the actual reference value returned by the `GetLatLongFromAddress()` method). – Peter Duniho Dec 07 '15 at 06:49
  • @PeterDuniho Always surprised how easily is solution. Thanks a lot! – Giorgi Pilishvili Dec 07 '15 at 06:50

0 Answers0