0

I am trying to get the latitude and longitude from the Google Geofencing API when using an address. I want to get the results in a JSON form and using the latitude and longitude from that. I have created classes by pasting with special from the JSON format when I go to the link I am trying to get information from.

My problem is that when I go to access the classes to get the latitude and longitude it throws an exception that says "Object Reference not set to an instance of an object" and "Speech_Recognition.GoogleGeoCodeResponse.geometry.get returned null".

Here is the code that is associated with it:

var url = "https://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";
var result = new System.Net.WebClient().DownloadString(url);
GoogleGeoCodeResponse googleResponse = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);



request = new ForecastIORequest("***Removed API KEY***", float.Parse(googleResponse.geometry.location.lat), float.Parse(googleResponse.geometry.location.lng), DateTime.Now, Unit.us); // Error is thrown here 
var response = request.Get();

And here is the data structures to deal with the JSON repsonse:

public class GoogleGeoCodeResponse
{
    public Result[] results { get; set; }
    public string status { get; set; }
}

public class Result
{
    public Address_Components[] address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public string[] types { get; set; }
}

public class Geometry
{
    public Bounds bounds { get; set; }
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Bounds
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Northeast
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Location
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Viewport
{
    public Northeast1 northeast { get; set; }
    public Southwest1 southwest { get; set; }
}

public class Northeast1
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest1
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Address_Components
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}  public string lng { get; set; }
}

Here is the content of the results:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Google Building 41",
           "short_name" : "Google Bldg 41",
           "types" : [ "premise" ]
        },
        {
           "long_name" : "1600",
           "short_name" : "1600",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Amphitheatre Parkway",
           "short_name" : "Amphitheatre Pkwy",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Mountain View",
           "short_name" : "Mountain View",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Santa Clara County",
           "short_name" : "Santa Clara County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "California",
           "short_name" : "CA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "94043",
           "short_name" : "94043",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 37.4228642,
              "lng" : -122.0851557
           },
           "southwest" : {
              "lat" : 37.4221145,
              "lng" : -122.0859841
           }
        },
        "location" : {
           "lat" : 37.4224082,
           "lng" : -122.0856086
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 37.4238383302915,
              "lng" : -122.0842209197085
           },
           "southwest" : {
              "lat" : 37.4211403697085,
              "lng" : -122.0869188802915
           }
        }
     },
     "place_id" : "ChIJxQvW8wK6j4AR3ukttGy3w2s",
     "types" : [ "premise" ]
  }
   ],
   "status" : "OK"
}

Why would I be getting this NULL exception thrown? How do I fix this?

Zack Sloan
  • 81
  • 12

1 Answers1

0

The geometry property is inside results.

Nick Larsen
  • 17,643
  • 6
  • 62
  • 94
  • So i just changed it to: `Result googleResponse = JsonConvert.DeserializeObject(result); request = new ForecastIORequest("ea3015a917dba9f4f679aa189febff24", googleResponse.geometry.location.lat, googleResponse.geometry.location.lng, DateTime.Now, Unit.us);` and got the same error. – Zack Sloan Jun 18 '17 at 17:21