# HG changeset patch # User IBBoard # Date 1259424366 0 # Node ID 89e26d51afc23413e01bb6f7fb55d97f98b4db7a # Parent 65553d2c86122334fab84a789ce7d7a5d519ef93 Fixes #226: "NullReferenceException" for a unitID * Add null check and throw InvalidFileException when getting unit XML by ID * Change InvalidDataException (which is a core exception about data streams) to InvalidFileException (which is our exception for incorrect data in a file) diff -r 65553d2c8612 -r 89e26d51afc2 api/Factories/Xml/WarFoundryXmlFactoryUtils.cs --- a/api/Factories/Xml/WarFoundryXmlFactoryUtils.cs Sun Nov 22 20:14:51 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactoryUtils.cs Sat Nov 28 16:06:06 2009 +0000 @@ -80,7 +80,7 @@ private static void ValidationEventMethod(object sender, ValidationEventArgs e) { - throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); + throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); } private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation) diff -r 65553d2c8612 -r 89e26d51afc2 api/Factories/Xml/WarFoundryXmlRaceFactory.cs --- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs Sun Nov 22 20:14:51 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs Sat Nov 28 16:06:06 2009 +0000 @@ -92,7 +92,14 @@ private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace) { - return GetUnitTypeForElement(WarFoundryXmlFactoryUtils.SelectSingleElement(doc, "/race:race/race:units/race:unit[@id='"+id+"']"), parentRace); + XmlElement unitWithId = WarFoundryXmlFactoryUtils.SelectSingleElement (doc, "/race:race/race:units/race:unit[@id='" + id + "']"); + + if (unitWithId == null) + { + throw new InvalidFileException("Could not find unit with ID "+id); + } + + return GetUnitTypeForElement(unitWithId, parentRace); } private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace) @@ -138,7 +145,7 @@ } catch (FormatException ex) { - throw new InvalidDataException(ex.Message, ex); + throw new InvalidFileException(ex.Message, ex); } string mainCatID = elem.GetAttribute("cat"); @@ -146,7 +153,7 @@ if (cat == null) { - throw new InvalidDataException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID)); + throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID)); } type.MainCategory = cat;