Reputation: 9080
This method:
private async Task readFileInfo(string folderId)
Has a call to another method:
importCount = await VM.importVehicles(myXDoc);
Defined here: (note: I changed it from a for loop to a for each but I'm getting similar results).
public async Task<Int32> importVehicles(XDocument importXMLDocument)
{
var Importedvehicles = from vehicle in importXMLDocument.Descendants("vehicle")
select new
{
VehicleName = vehicle.Element("VehicleName").Value,
VehicleYear = vehicle.Element("VehicleYear").Value,
Odometer = vehicle.Element("Odometer").Value,
LicensePlate = vehicle.Element("LicensePlate").Value,
OilWeight = vehicle.Element("OilWeight").Value,
OilQuantity = vehicle.Element("OilQuantity").Value,
OilFilterModelNumber = vehicle.Element("OilFilterModelNumber"),
AirFilterModelNumber = vehicle.Element("AirFilterModelNumber"),
OilChangedDate = vehicle.Element("OilChangedDate"),
OilChangedOdometer = vehicle.Element("OilChangedOdometer"),
NextOilChangeDate = vehicle.Element("NextOilChangeDate"),
NextOilChangeOdometer = vehicle.Element("NextOilChangeOdometer"),
SettingDistance = vehicle.Element("SettingDistance"),
SettingMonths = vehicle.Element("SettingMonths"),
};
Int32 vehicleId;
vehicleId = await getMaxVehicleId();
try
{
foreach (var item in Importedvehicles)
{
vehicle myImportedVehicle = new vehicle();
myImportedVehicle.VehicleId = vehicleId += 1;
myImportedVehicle.ImagePath = "Assets/car2.png";
myImportedVehicle.VehicleName = item.VehicleName;
myImportedVehicle.VehicleModel = item.VehicleName;
myImportedVehicle.VehicleYear = short.Parse(item.VehicleYear);
myImportedVehicle.CurrentOdometer = Convert.ToInt32(item.Odometer);
myImportedVehicle.LicensePlate = item.LicensePlate;
myImportedVehicle.LastOilChangedDate = Convert.ToDateTime(item.OilChangedDate.Value.ToString()).ToString("d");
myImportedVehicle.LastOilChangedOdometer = (Int32)item.OilChangedOdometer;
myImportedVehicle.ReminderDistance = (Int32)item.SettingDistance;
myImportedVehicle.ReminderMonths = (Int32)item.SettingMonths;
vehicleInformation myImportVI = new vehicleInformation();
myImportVI.OilWeight = item.OilWeight;
myImportVI.OilAmount = item.OilQuantity;
myImportVI.OilFilterNumber = item.OilFilterModelNumber.Value.ToString();
myImportVI.AirFilterNumber = item.AirFilterModelNumber.Value.ToString();
myImportedVehicle.vehicleInfo = myImportVI;
m_vehicles.Add(myImportedVehicle);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
}
await SaveList();
return Importedvehicles.Count();
}
I'm getting an error:
Object reference not set to an instance of an object.
when I step through it the iVehicle
is highlighted but then it goes directly to the for statement. Then it errors as it looks as it hasn't gotten the results from iVehicle
yet.
Upvotes: 0
Views: 481
Reputation: 65079
This doesn't 100% answer your question, but it should give you a good start.
The reason the debugger jumps straight into the for...loop
after the declaration of iVehicle
, is because your query does not get executed when you declare it. Therefore, iVehicle at that point is not a collection of anonymous types.
When you call .Count()
, the query is being executed and iVehicle
is attempting to be turned into a proper collection of anonymous types. However, because something in the query (that is being executed after you call .Count()
) is null
, you're receiving an NullReferenceException
.
You should start by verifying that both importXMLDocument
and the return value from the call to Descendants()
is not null.
Hope that helps anyway.
EDIT:
Now that you've given a complete example, you have heaps of places that could potentially be null
.
Each time you use this:
vehicle.Element("SomeElementNameHere")
That could potentially be null
. Then you're calling the .Value
property on a null
object.
You need to make sure each element is definitely there. Isolate each case, and determine which one is null
.
Upvotes: 1
Reputation: 117074
Try writing your code like this:
var query =
from vehicle in importXMLDocument.Descendants("vehicle")
select new { ... };
var iVehicle = query.ToArray();
for (var i = 0; i <= iVehicle.Count(); i++)
{
...
}
You need to force the evaluation of the query. That's what the .ToArray
is doing. The query itself is only the definition of the query, not the results.
Upvotes: 1