I have a few for loops to determine the distances between a selected location (in this code, 2 application settings) and a list of around 5000 other locations using the Haversine formula (in this code, a function named 'Formula' in a class file named 'Haversine'). The loop works how I want it to and is completely fine until it gets to the second last 'record' in the file at which point it stops and throws an error. I was wondering if anyone would help me fix it. The code is below but without the function as it is in a separate file.
static void Main(string[] args)
{
XmlDocument SiteList = new XmlDocument();
SiteList.Load("C:/Users/Henry/WeatherWatcher/Development/sitelist.xml");
List<string> list = new List<string>();
Console.WriteLine("HAVERSINE: BASE LA-" + Properties.Settings.Default.LATITUDE + "LO-" + Properties.Settings.Default.LONGITUDE);
for (int x = 0; x < SiteList.SelectNodes("/Locations/Location/@id").Count; x++)
{
double ListLO = Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@longitude")[x].InnerText);
for (int y = 0; y < SiteList.SelectNodes("/Locations/Location/@latitude").Count; x++ )
{
double ListLA = Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@latitude")[x].InnerText);
string Item = Convert.ToString(Haversine.Formula(Convert.ToDouble(52.2802588), Convert.ToDouble(Properties.Settings.Default.LONGITUDE), ListLA, ListLO) + "---" + SiteList.SelectNodes("/Locations/Location/@id")[x].InnerText);
Console.WriteLine(Item);
list.Add(Convert.ToString(Item.ToString()));
Console.WriteLine("");
Console.WriteLine(x);
}
Console.ReadKey();
}
}
The xml file it is reading is structured like this: (with one '<location'>
="1.0"="ISO-8859-1"
-<Locations>
<Location name="Rosehearty Samos" longitude="-2.121" latitude="57.698" id="3094"/>
<Location name="Strathallan" longitude="-3.729" latitude="56.326" id="3144"/>
<Location name="Edinburgh/Gogarbank" longitude="-3.343" latitude="55.928" id="3166"/>
...
<Location name="Rhossili Bay (Beach)" longitude="-4.2905" latitude="51.5707" id="354477"/>
<Location name="Perranporth - Village (Beach)" longitude="-5.1535" latitude="50.3491" id="354541"/>
<Location name="Perranuthnoe (Perran Sands) (Beach)" longitude="-5.4411" latitude="50.1121" id="354552"/>
</Locations>
The number of locations I want to process is 5363 (the total number in the file) however it stops and give an error leaving the console output as:
...
5359
87.7415106490408---354477
5360
218.280932624672---354541
5361
244.281295790985---354552
5362
What it should do is process all 5363 locations and display them then close. It hasn't processed the second last location and the whole of the last one giving the error:
System.NullReferenceException was unhandled
Object reference not set to an instance of an object.
I don't know what to do really so I was wondering if one of you could help get the loops working. I am writing is C# using MS VS2010. I hope this makes sense and thank you for any help.