2

I'm trying to create a list in a class named 'Advisers' that would hold a list of students who are advised by 1 instance of an adviser. I have a student class created, and have no trouble there, but I get errors in my adviser class on runtime.

class Advisers
{
    private string _Fname;
    private string _Lname;
    private string _Department;
    private List<Student> _StudentsAdvised;   

    //constructor
    public Advisers()
    {
        _Fname = "";
        _Lname = "";
        _Department = "";
        _StudentsAdvised = null;
    }

    public void AddToList(Student newStudent)
    {
        _StudentsAdvised.Add(newStudent);
    }

    public List<Student> StudentsAdvised
    {
        get { return _StudentsAdvised; }
        set { _StudentsAdvised = value; }
    }

    public string FirstName
    {
        get { return _Fname; }
        set { _Fname = value; }
    }

    public string LastName
    {
        get { return _Lname; }
        set { _Lname = value; }
    }

    public string Department
    {
        get { return _Department; }
        set { _Department = value; }
    }       
}

The AddToList method is what I call in the main form to add an instance of a student to the StudentsAdvised list held in the Adviser class.

private void stuAssignAdviBtn_Click(object sender, EventArgs e)
    {
        int stuIndex = stuLstbox.SelectedIndex;
        int advIndex = adviListBox.SelectedIndex;

        if (stuLstbox.SelectedIndex != -1 && adviListBox.SelectedIndex != -1)
        {
            string AdvLastName = AdviserList[advIndex].LastName;
            StudentList[stuIndex].AdviserLastName = AdvLastName;


            AdviserList[advIndex].AddToList(StudentList[stuIndex]);
        }
        else
        {
            MessageBox.Show("Select 1 student and 1 adviser.");
        }
    }

The error messages comes up in the Adviser class here:

public void AddToList(Student newStudent)
{
    _StudentsAdvised.Add(newStudent);
}

How can I do this?

3
  • 1
    wouln't they be advisees?
    – Jodrell
    Nov 26, 2013 at 16:38
  • 1
    change _StudentsAdvised = null; in your constructor to _StudentsAdvised = new List<Student>(); Nov 26, 2013 at 16:39
  • the List could be BindingList...at least...or derive a class from bindinglist<Student>(or a generic one) with the proper implementations you need then in your adviser class use that derived from bindinglist class to hold the information... Nov 26, 2013 at 16:50

2 Answers 2

2

I dont see you initializing your List<Student> _StudentsAdvised anywhere

you could do that in the class constructor

//constructor
public Advisers()
{
    _Fname = "";
    _Lname = "";
    _Department = "";
    _StudentsAdvised = new List<Student>();
}
2
  • 1
    agreed - you are NOT newing up your collection in the constructor. you are just making it null thus throws a nullReferenceException when you try to use that object Nov 26, 2013 at 16:39
  • 1
    My (noob) understanding of the constructor was that it initializes the values. I wanted to start the list empty, which is why I set it to null. This worked though, thank you!
    – cjharty
    Nov 26, 2013 at 16:54
0

erm,

public class Advisess : List<Student>
{
    private readonly Advisor advisor;

    private Advisess()
    {
    }

    public (IEnumerable<Student> adivsees, Advisor advisor)
        : base(advisees)
    {
        this.advisor = advisor;
    }

    public (Advisor advisor)
    {
        this.advisor = advisor;
    }

    public Advisor Advisor
    {
        get
        {
            return this.advisor;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.