简体   繁体   中英

Why does this code throws NullReferenceException?

public FileEntry ReadFileConfiguration(string id)
{
    string configurationPath = "conf.xml";
    XDocument data = XDocument.Load(configurationPath);
    return (from c in data.Descendants("file")
        where (c.Attribute("Id") != null && c.Attribute("Id").Value.Equals(id)) 
        select new FileEntry()
        {
            Name = c.Element("Name").Value,
            Path = c.Element("Path").Value,
            SheduledTime = Convert.ToDateTime(c.Element("SheduledTime").Value),
            Size = (long)Convert.ToDouble( c.Element("Size").Value),
            IsFolder = Convert.ToBoolean( c.Element("IsFolder").Value),
            LastAccess = Convert.ToDateTime(c.Element("LastAccess").Value),
            DoEncrypt = Convert.ToBoolean( c.Element("DoEncrypt").Value)
        }).First();
}

Main program is:

main()
{
    string id  = "C:\\Users\\Radhesh\\Documents\\Visual Studio 2008\\Projects\\Rme\\Rme\\test.txt";
    ReadFileConfiguration(id);
}

My XML page is:

<?xml version="1.0" encoding="utf-8" ?> 
<Files>
    <file Id="C:\Users\Radhesh\Documents\Visual Studio 2008\Projects\Rme\Rme\test.txt">
        <Name>test.txt</Name> 
        <Path>C:\Users\Radhesh\Documents\Visual Studio 2008\Projects\Rme\Rme\test.txt</Path> 
        <IsFolder>False</IsFolder> 
        <DoEncrypt>True</DoEncrypt> 
        <Size>0</Size> 
        <LastAcess>7/9/2011 11:35:53 PM</LastAcess> 
        <SheduledTime>7/10/2011 1:59:20 PM</SheduledTime> 
    </file>
</Files>

My class:

class FileEntry
{
        public string Name { get;set;}
        public string Path { get; set; }
        public bool IsFolder { get; set; }
        public long Size { get;set; }
        public DateTime LastAccess { get;set; }
        public DateTime SheduledTime { get; set; }
        public bool DoEncrypt { get;  set; }

}

Please, can anyone help?

Edit, just spotted it

 <LastAcess>7/9/2011 11:35:53 PM</LastAcess> 

 LastAccess = Convert.ToDateTime(c.Element("LastAccess").Value),

Notice: "Acess" and Access


Start by breaking it down.

string configurationPath = "conf.xml";
XDocument data = XDocument.Load(configurationPath);
var files = from c in data.Descendants("file") select c;

Debug.WriteLine("Count = {0}", files.Count());

...
return files.First();

This should output 1 (or higher).

If it succeeds, add the where clause before the select .

Then change to the full select part and add the Name = c.Element("Name").Value, lines 1 by 1.

I followed the steps that @Henk Holterman suggested, and here's why you're seeing the Null Reference Exception. In your select:

LastAccess = Convert.ToDateTime(c.Element("LastAccess").Value)

If you look at your XML file, you have:

<LastAcess>7/9/2011 11:35:53 PM</LastAcess>

You have, in a nutshell, a typo. The select portion of the query couldn't find an element named "LastAccess", therefore throwing the Null Reference Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM