1

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.

Viking
  • 465
  • 3
  • 15

2 Answers2

3

a better way to trace the error is

foreach (var element in elementen.Elements("Proces"))
        {
            Process process = new Process();


             process.Naam = element.Element("Naam").Value;
             process.TemplatePath = element.Element("TemplatePath").Value;
             process.OutputPath = element.Element("OutPutPath").Value;
             process.OutputDocumentName = element.Element("OutputDocumentName").Value;
             lijst.Add(process)
        }

and add a break point to check the code line by line

Usman
  • 4,615
  • 2
  • 17
  • 33
2

Your list is instantiated and List.Add() will work fine. The problem is here:

new Process
{
    Naam = element.Element("Naam").Value,
    TemplatePath = element.Element("TemplatePath").Value,
    OutputPath = element.Element("OutPutPath").Value,
    OutputDocumentName = element.Element("OutputDocumentName").Value
});

Check every line of this code to be sure all operations return expected results.

Roman
  • 11,966
  • 10
  • 38
  • 47
  • Hey thanks, I misspelled OutPutPath should have been OutputPath. Why does the stacktrace/exception info does not tell me anything about that value but instead points me to list.add as point where the exception occured? – Viking May 11 '16 at 07:12
  • 1
    @TerryvandenBerg, it is because you create object inside Add() method. You can see Usman answer - if you create process object outside Add() method the exception will occur in line with OutputPath. – Roman May 11 '16 at 07:16