简体   繁体   中英

Why does this throw a NullReferenceException?

private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
    XDocument xml = XDocument.Load(this.dir + xmlFile);

    if (xml.Element(parent).Element(node).Value != null)
    {
        xml.Element(parent).Element(node).Value = newVal;
    }
    else
    {
        xml.Element(parent).Add(new XElement(node, newVal)); 
    }

    xml.Save(dir + xmlFile); 
}  

Why does this throw

System.NullReferenceException was unhandled by user code

on this line

if (xml.Element(parent).Element(node).Value != null)

?

I'm guessing it's because the XML node doesn't exist, but that's what the != null is suppose to check for. How do I fix that?

I've tried several things and they ALL throw the same exception at some point during the not null check.

Thanks for any help.

Either xml.Element(parent) or the Element(node) you are trying to access from the return value of xml.Element(parent) is null .

Restructuring like this will enable to you see which one it is:

var myElement = xml.Element(parent);
if (xmyElement != null)
{
    var myNode = myElement.Element(node);
    if(myNode != null)
    {
      myNode.Value = newVal;
    }
}

Update:

From your comment it looks like you want to do this:

if (xml.Element(parent).Element(node) != null)  // <--- No .Value
{
    xml.Element(parent).Element(node).Value = newVal;
}
else
{
    xml.Element(parent).Add(new XElement(node, newVal)); 
}

几乎可以肯定,因为它返回null:

xml.Element(parent)

You need to check if:

Element(node) != null

Before you call .Value. If Element(node) == null, then the call to .Value will throw a null reference exception.

Dan

Try changing your if statement to this:

if (xml.Element(parent).Element(node) != null)

If the node in the parent element is null, you cannot access a member of a null object.

At least:

private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
    XDocument xml = XDocument.Load(this.dir + xmlFile);
    XElement parent = xml.Element(parent).Element(node);
    if (parent  != null)
    {
        parent.Value = newVal;
    }
    else
    {
        xml.Element(parent).Add(new XElement(node, newVal)); 
    }    
    xml.Save(dir + xmlFile); 
}  

Better:

private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
    string path = System.IO.Path.Combine(dir, xmlFile);
    XDocument xml = XDocument.Load(path );
    XElement parent = xml.Element(parent).Element(node);
    if (parent != null)
    {
        XElement node = parent.Element(parent);
        if (node != null)
        {
            node.Value = newVal;
        }
        else
        {
            // no node
        }
    }
    else
    {
        // no parent
    }    
    xml.Save(path); 
}  
        if (xml.Element(parent) != null)
        {
            var myNode = xml.Element(parent).Element(node);
            if (node != null)
                myNode.Value = newVal;
        }
        else
        {
            xml.Element(parent).Add(new XElement(node, newVal));
        }

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