0

I am using googlemaps apis to get location name using lat and long provided by user it works on local machine but the problem is that it doesn't work when application is deployed. It gives null exception here is my code.

public static string GETGoogleAddress(double lat, double longi)
{
    string URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + longi+ "&key=AIza*****-Lyb****_********efVUW1o7B****";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
    try
    {
        WebResponse response = request.GetResponse();
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            JObject parseJson = JObject.Parse(reader.ReadToEnd());
            var getJsonres2 = parseJson["results"][1];
            var getAddress2 = getJsonres2["formatted_address"];
            string _Address = getAddress2.ToString();
            return _Address;
        }
    }
    catch (WebException ex)
    {
        WebResponse errorResponse = ex.Response;
        using (Stream responseStream = errorResponse.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            // log errorText
        }
        throw;
    }
}

I am calling this utility helper like this

 var _location = UtilityHelper.GeoAddressUtilHelper.GETGoogleAddress(model.Latitude??0, model.Longitude??0);

which doesn't return any location in production. It works fine on local but not working windows server when application is deployed.

Following is the stackTrace i am getting

at Offey.services.UtilityHelper.GeoAddressUtilHelper.GETGoogleAddress(Double lat, Double longi)

Exception Type

NullReferenceException

Exception message

Object reference not set to an instance of an object.

Shamshad Jamal
  • 19
  • 3
  • 17
  • 4
    Please add details of exactly *where* it's throwing an exception, and what steps you've taken to diagnose the problem. (Most NRE questions are closed as a duplicate of https://stackoverflow.com/questions/4660142 - basically you need to work out what's null, and your question should be about why that's null.) – Jon Skeet Jan 28 '19 at 07:24
  • Also note that using `ToString()` here for `double` values isn't a great idea - you should specify the invariant culture to ensure that the values are converted to strings using `.` as the decimal separator. – Jon Skeet Jan 28 '19 at 07:24
  • When i call GETGoogleAddress method by passing lat and long it gives me exception on production. By production i mean when i deploy the application on remote server using IIS. – Shamshad Jamal Jan 28 '19 at 07:34
  • Right, so what does the stack trace look like? If you haven't got diagnostics set up so that you can get at the exception stack trace and use that to help diagnose what's going on, that's probably the most important thing to address. – Jon Skeet Jan 28 '19 at 07:37
  • at Offey.services.UtilityHelper.GeoAddressUtilHelper.GETGoogleAddress(Double lat, Double longi) I am logging the following error in db. – Shamshad Jamal Jan 28 '19 at 07:44
  • Rather than adding comments, please *edit the question* to include the full stack trace. – Jon Skeet Jan 28 '19 at 07:45
  • Please view the updated question. – Shamshad Jamal Jan 28 '19 at 07:48
  • That's not a complete stack trace - and it doesn't have a line number in, which makes it very hard to help more. If you need to deploy a release build without line numbers, please add logging so that you can tell exactly which line is causing the problem. You might also want to log the JSON you've received. – Jon Skeet Jan 28 '19 at 08:14
  • The url is causing the error because when i run the exact url in google chrome on server it gives me no response rather then just loading try to access it but no result – Shamshad Jamal Jan 28 '19 at 08:17
  • I'm afraid I didn't follow that comment. If you've worked out what's null, then ideally convert this into a [mcve] that shows something being null when you don't expect it to be. – Jon Skeet Jan 28 '19 at 12:26

0 Answers0