0

我有一个三类:

首先是人:

    public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }



    public Person()
    {
    }

    public override string ToString()
    {
        return FirstName + " " +LastName + "\t" +Email;
    }
}

二是:

     public class Student:Person
{

    public Student()
    {
    }
    public double AssessmentGrade { get; set; }
    public double AssignmentGrade { get; set; }

    public override string ToString()
    {
        return base.ToString() + "," +AssessmentGrade + "," + AssignmentGrade;
    }
}

第三是:

   public class Teacher:Person
{
    public int RoomNumber
    {
        get;
        set;
    }

    public override string ToString()
    {
        return base.ToString() + "," + RoomNumber;
    }
}

我还有一门课,我只是从 PeronDB (类)中调用数据

    public class Persons
{
     private List<Person> persons = null;

       public void Fill()
    {
        persons = PersonDB.GetPersons();
    }
 }

InPersonDB 类如果它的 Student 节点然后读取数据,如果它的 Teacher 节点也读取数据并且如果它 Person 它也读取数据,我正在做。

    public class PersonDB
{
    private const string path = @"..\..\Persons.xml";

    public static List<Person> GetPersons()
    {
        List<Person> persons = new List<Person>();

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        settings.IgnoreComments = true;

        XmlReader xmlIn = XmlReader.Create(path, settings);

        if (xmlIn.ReadToDescendant("Student"))
        {
            do
            {
                Person person = new Person();
                Student student = (Student)person;
                xmlIn.ReadStartElement("Student");
                student.FirstName = xmlIn.ReadElementContentAsString();
                student.LastName = xmlIn.ReadElementContentAsString();
                student.Email = xmlIn.ReadElementContentAsString();
                student.AssessmentGrade = xmlIn.ReadElementContentAsInt();
                student.AssignmentGrade = xmlIn.ReadElementContentAsInt();

                persons.Add(student);
            } while (xmlIn.ReadToNextSibling("Student"));

        }

        else if (xmlIn.ReadToDescendant("Teacher"))
        {
            do
            {
                Person person = new Person();
                Teacher teacher = (Teacher)person;
                xmlIn.ReadStartElement("Teacher");
                teacher.FirstName = xmlIn.ReadElementContentAsString();
                teacher.LastName = xmlIn.ReadElementContentAsString();
                teacher.Email = xmlIn.ReadElementContentAsString();
                teacher.RoomNumber = xmlIn.ReadElementContentAsInt();

                persons.Add(teacher);
            } while (xmlIn.ReadToNextSibling("Teacher"));
        }

        if (xmlIn.ReadToDescendant("Person"))
        {
            do
            {
                Person person = new Person();

                xmlIn.ReadStartElement("Person");
                person.FirstName = xmlIn.ReadElementContentAsString();
                person.LastName = xmlIn.ReadElementContentAsString();
                person.Email = xmlIn.ReadElementContentAsString();

                persons.Add(person);
            } while (xmlIn.ReadToNextSibling("Person"));
        }


        xmlIn.Close();

        return persons;
    }
   } 

在表单加载中我正在做:

     Persons personList = null;

        private void FillPersonlstBox()
    {
        Person p;
        listBox1.Items.Clear();
        for (int i = 0; i < personList.Count; i++)
        {
            p = personList[i];
            listBox1.Items.Add(p.ToString());
        }
    }

    private void Load_Click(object sender, EventArgs e)
    {
        personList.Changed += new Persons.ChangeHandler(HandleChange);
        personList.Fill();
        FillPersonlstBox();
    }

它显示在 ** personList.Changed += new Persons.ChangeHandler(HandleChange); ** 此行错误。它显示“对象引用未设置为对象的实例。” 这个错误。请各位大神帮帮我如何解决?

4

1 回答 1

3

在某些时候,您必须实例化personList.

在您的代码中,您无法处理ChangedpersonList 上的事件,因为它仍然为空。

所以而不是

Persons personList = null;

也许试试

Persons personList = new Persons();
于 2012-08-16T02:44:36.987 回答