Im trying to add objects called Process to the list. It returns a null reference exception when I try to add items to the list. (lijst.Add)
I search the net and find mostly answers like: you dont have instantiated the list. Well here it is instantiated so what could be wrong with this code? All other variables are filled correctly.
This is the code:
private List<Process> CreateProcessFromXml()
{
List<Process> lijst = new List<Process>();
var path = Path.Combine(Server.MapPath("~/App_Data"), "Process.xml");
XDocument process = XDocument.Load(path);
var elementen = XElement.Load(path);
foreach (var element in elementen.Elements("Proces"))
{
lijst.Add(
new Process
{
Naam = element.Element("Naam").Value,
TemplatePath = element.Element("TemplatePath").Value,
OutputPath = element.Element("OutPutPath").Value,
OutputDocumentName = element.Element("OutputDocumentName").Value
});
}
return lijst;
}
Process class:
public class Process
{
public string Naam { get; set; }
public string TemplatePath { get; set; }
public string OutputPath { get; set; }
public string OutputDocumentName { get; set; }
}
Exception tekst:
Line 34: foreach (var element in elementen.Elements("Proces"))
Line 35: {
Line 36: lijst.Add(new Process (in red)
Line 37: {
Line 38: Naam = element.Element("Naam").Value,
Source File: xxxxxxxx Line: 36
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
XML: <Proces>
<Naam>
</Naam>
<TemplatePath>
</TemplatePath>
<OutputPath>
</OutputPath>
<OutputDocumentName>
</OutputDocumentName>
</Proces>
Thanks in advance.