Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Google Maps API对随机行执行NullReferenceException_C#_Sql_Xml_Google Maps - Fatal编程技术网

C# 使用Google Maps API对随机行执行NullReferenceException

C# 使用Google Maps API对随机行执行NullReferenceException,c#,sql,xml,google-maps,C#,Sql,Xml,Google Maps,我得到了一个奇怪的例外。我做了以下工作: 从SQL Server数据库中获取streetname、number和city。 我使用SqlDataReader读取结果,并将其插入谷歌地图API的URL中 接下来,我将其加载到一个XML文档中,并获取经度和纬度的值 我的作品都很好,但它永远不会结束。共有417个不同的行。有时在ish第25行,有时在ish第300行,会出现NullReferenceException public void calcLatLong(SqlCommand sql)

我得到了一个奇怪的例外。我做了以下工作: 从SQL Server数据库中获取streetname、number和city。 我使用
SqlDataReader
读取结果,并将其插入谷歌地图API的URL中

接下来,我将其加载到一个XML文档中,并获取经度和纬度的值

我的作品都很好,但它永远不会结束。共有417个不同的行。有时在ish第25行,有时在ish第300行,会出现
NullReferenceException

public void calcLatLong(SqlCommand sql)
    {
        sql.Connection = connection;
        SqlDataReader reader = null;
        reader = sql.ExecuteReader();
        String finalread = "";
        while (reader.Read())
        {
            finalread = reader[0] + "" + reader[1] + "" + reader[2];

            //URL for using Google API
            String url = "http://maps.google.com/maps/api/geocode/xml?address=" + finalread + "&sensor=false";
            //XML format for scraping webpage
            XmlDocument objXmlDocument = new XmlDocument();
            objXmlDocument.Load(url);

            latitude = objXmlDocument.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat/text()").Value;
            longitude = objXmlDocument.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng/text()").Value;
            Console.Write(latitude + " ");

Console.WriteLine(longitude);
    }
    reader.Close();
}
错误消息是:

An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication1.exe
在这一行:

latitude = objXmlDocument.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat/text()").Value;
但这是这一行,因为这是从URL中选择内容的第一行

我想可能是无线网络。我通过
cmd.exe
ping
google.com尝试了一百次,结果丢失了一个数据包。所以这会立即产生一个错误,对吗


谢谢你的帮助

你能发布整个错误消息吗?查看抛出错误的行号或错误涉及的对象等会有所帮助。我强烈感觉问题在于您正在检索的数据。我添加了它。我猜URL没有正确加载。这就是为什么在
objXmlDocument.Load(url)之后的第一行
返回一个
null
值。但这只是我的猜测。好吧,那么我确定xml中该节点的值为null。您应该做的是尝试在执行过程中抛出错误时在该行中断,并尝试查看XML的外观。如果xml有一个与您要查找的节点类似的节点,并且它的值不为null,则说明其他问题,但如果该节点为null,则说明您创建的xml是错误的,如果该值为null,然后,您所要做的就是放入一些逻辑来处理null值,因为这意味着您的数据库中有一个null值。但是字符串的值永远不会是
null
,除非返回
null
。如果I ping(在本例中是google.com),那么有时is会给出500毫秒以上的响应。我不知道这是否会影响它?我不认为我的XML加载是错误的,因为它以随机值给出异常。这肯定是一个很长的ping,但我确信您的问题至少在某种程度上与您的XML有关,因为这就是您发布的行出错的原因。