简体   繁体   English

代码中的空引用异常

[英]null reference exception in the code

I am getting NullReferenceException error on "_attr.Append(xmlNode.Attributes["name"]);". 我在“ _attr.Append(xmlNode.Attributes [” name“]);”上收到NullReferenceException错误。

namespace SMAS
{
class Profiles
{
    private XmlTextReader _profReader;
    private XmlDocument _profDoc;

    private const string Url = "http://localhost/teamprofiles.xml";
    private const string XPath = "/teams/team-profile";

    public XmlNodeList Teams{ get; private set; }
    private XmlAttributeCollection _attr;

    public ArrayList Team { get; private set; }

    public void GetTeams()
    {
        _profReader = new XmlTextReader(Url);
        _profDoc = new XmlDocument();

        _profDoc.Load(_profReader);
        Teams = _profDoc.SelectNodes(XPath);

        foreach (XmlNode xmlNode in Teams)
        {
            _attr.Append(xmlNode.Attributes["name"]);
        }
    }
}
}

the teamprofiles.xml file looks like teamprofiles.xml文件看起来像

  <teams>
    <team-profile name="Australia">
      <stats type="Test">
        <span>1877-2010</span>
        <matches>721</matches>
        <won>339</won>
        <lost>186</lost>
        <tied>2</tied>
        <draw>194</draw>
        <percentage>47.01</percentage>
      </stats>
      <stats type="Twenty20">
        <span>2005-2010</span>
        <matches>32</matches>
        <won>18</won>
        <lost>12</lost>
        <tied>1</tied>
        <draw>1</draw>
        <percentage>59.67</percentage>
      </stats>
    </team-profile>
    <team-profile name="Bangladesh">
      <stats type="Test">
        <span>2000-2010</span>
        <matches>66</matches>
        <won>3</won>
        <lost>57</lost>
        <tied>0</tied>
        <draw>6</draw>
        <percentage>4.54</percentage>
      </stats>
    </team-profile>
 </teams>

I am trying to extract names of all teams in an ArrayList. 我试图提取ArrayList中所有团队的名称。 Then i'll extract all stats of all teams and display them in my application. 然后,我将提取所有团队的所有统计信息并将其显示在我的应用程序中。 Can you please help me about that null reference exception? 您能帮我解决该空引用异常吗?

I cant see where you initialise private XmlAttributeCollection _attr; 我看不到您在哪里初始化private XmlAttributeCollection _attr;

you can try 你可以试试

 _profDoc.Load(_profReader);
_attr =  _profDoc.DocumentElement.Attributes;

You never initialize _attr . 您永远不会初始化_attr It's a null reference. 这是一个空引用。

As others have said, you have to initialize _attr. 正如其他人所说,您必须初始化_attr。

With what value? 具有什么价值?

A XmlAttributeCollection is returned by XmlElement.Attributes . XmlAttributeCollectionXmlElement.Attributes返回。 If what you want is the attributes of an element, you can just use that property. 如果您想要的是元素的属性,则可以使用该属性。 If what you want is a "collection of XmlAttribute " but not forcibly a XmlAttributeCollection , you can declare it like this: 如果您想要的是“ XmlAttribute集合”而不是XmlAttributeCollection强行,则可以这样声明:

ICollection<XmlAttribute> _attr = new List<XmlAttribute>();

And then use ICollection<T>.Add instead of Append . 然后使用ICollection<T>.Add代替Append

Or, use LINQ: 或者,使用LINQ:

_attr = (from node in Teams
        select node.Attributes["name"]).ToList();

似乎您从未给_attr分配任何_attr ,因此它肯定为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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