I’m getting the NRE error that says: “Object reference not set to an instance of an object.”
From the following code:
select new
{
ICAO = station.Element("icao").Value,
};
The entire script is:
XDocument xmlDoc = XDocument.Load(@"http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=94107");
var stations = from station in xmlDoc.Descendants("station")
select new
{
ICAO = station.Element("icao").Value,
};
lblXml.Text = "";
foreach (var station in stations)
{
lblXml.Text = lblXml.Text + "ICAO: " + station.ICAO + "<br />";
}
if (lblXml.Text == "")
lblXml.Text = "No Results.";
}
I don’t understand why it isn’t creating the station object and setting the ICAO value. Any ideas/tips for future XML and C# reference?
It appears that only the airport stations have the icao element. This should work for you:
You could instead add a where condition to get around the exception:
Also, you can pull a value like this to prevent an exception, though it will return numerous null records you may or may not want:
You can do that for various other types, not only for strings.