views:

238

answers:

1

I'm getting a null reference exception whenever it tries to add the packages titles info and other attributes but the attributes exist and the proper package is selected

Heres the code:

private void categorylist_listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            XmlDocument LoadPackageList = new XmlDocument();
            //Removes the text "Select A Category" and refrehes the form
            packagelist_listbox.Items.Remove(SelectaCategory_listbox);

            if (categorylist_listview.SelectedItem == WWW_listviewitem)
            {
                LoadPackageList.Load("www.xml");
                XmlNodeList WWWPackageList = LoadPackageList.SelectNodes("/Packages/*");
                int countthenodes = 0;
                foreach (XmlNode WWWPackages in WWWPackageList)
                {
                    //Cycles through all the packages and assings them to a string then adds it to the packagelist
                    countthenodes++;
                    PackageTitle[countthenodes] = WWWPackages.Attributes["title"].ToString();
                    PackageInfo[countthenodes] = WWWPackages.Attributes["info"].ToString();
                    PackageDownloadUrl[countthenodes] = WWWPackages.Attributes["downloadurl"].ToString();
                    PackageTags[countthenodes] = WWWPackages.Attributes["tags"].ToString();
                    packagelist_listbox.Items.Add(PackageTitle[countthenodes]);
                }
                Refresh(packagelist_listbox);

            }
        }

It Errors out at PackageTitle[countthenodes] = WWWPackages.Attributes["title"].ToString();

XML File:

<Packages>
  <Firefox title="Mozilla Firefox" tags="www firefox web browser mozilla" info="http://google.com" downloadurl="http://firefox.com"&gt;&lt;/Firefox&gt;


</Packages>

The Variables are declared

        public string[] PackageTags;
        public string[] PackageTitle;
        public string[] PackageInfo;
        public string[] PackageDownloadUrl;

At the very beginning of the file

+2  A: 

Well, the first problem is that calling ToString() on an XmlAttribute isn't going to do what you want. You should use the Value property. However, I don't believe that's causing a NullReferenceException unless the data isn't quite as you showed it. Here's a short but complete program which works fine:

using System;
using System.Xml;

class Test
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("test.xml");        
        XmlNodeList list = doc.SelectNodes("/Packages/*");
        foreach (XmlNode node in list)
        {
            Console.WriteLine(node.Attributes["title"].Value);
        }
    }
}

That displays "Mozilla Firefox" with the XML you gave us.

Options:

  • Your real XML actually contains an element without a title attribute
  • Perhaps PackageTitle is null?

It would help if you could produce a short but complete program demonstrating the problem. Ideally it should avoid using a GUI - I can't see anything here which is likely to be GUI-specific.

If you could tell us more about PackageTitle and how it's being initialized, that would help too. How are you expecting it to just keep expanding for as many elements as you find? Or is it an array which is initialized to a larger size than you ever expect to find elements?

Jon Skeet
I added MessageBox.Show(WWWPackages.Attributes["title"].Value); to the beginning of the foreach statement and it displays in a messagebox "Mozilla Firefox" and then it crashes
Indebi
The Variables are declared like so:public string[] PackageTags; public string[] PackageTitle; public string[] PackageInfo; public string[] PackageDownloadUrl;
Indebi
@Indebi: So have you ever actually *initialized* those variables? If not, they'll be null - hence the problem. It's got nothing to do with XML or ListBoxes - just to do with working with variables in general and arrays in particular. I would actually suggest that you create a new `Package` type with members for title, info, tags and download URL - then have a `List<Package>` which you can add elements to.
Jon Skeet