1

In order to navigate my application you are presented with the Mainwindow on load up. There is a button that you can click that opens the TeacherTools window. From here you are able to add new students to the list. On this window I have a back button that will return you to the MainWindow and I can confirm that the information is still usable here. I'm having an issue whereby I open a 3rd window which is a Exam window in which students are presented with questions afterwards and their score should be added to the currently loaded Student visa his name.

private List<Student> studentList = new List<Student>();

public partial class MainWindow : Window
{
    TeacherTools teachTools = new TeacherTools();
    Learning myLearning = new Learning();

    private void teachAdmin_Click(object sender, RoutedEventArgs e)
    {
        this.Hide();
        teachTools.ShowDialog();
        this.Show();
    }

    private void Selection_Click(object sender, RoutedEventArgs e)
    {
        this.Hide();
        myTest.studentName.Text = studentsList.SelectedValue.ToString();
        myTest.ShowDialog();
        this.Show();
    }

}

//Exam Window
public partial class Test : Window
{
    //I'm sure its not this
    Student loadedStudent = new Student();
    TeacherTools teachTools = new TeacherTools();

    public void(private void finishTest()
    {

        loadedStudent = teachTools.Kids.Find(delegate(Student s) { return s.Name == studentName.Text; }); //This line 
        loadedStudent.Attempted = true;
        loadedStudent.Score = score;
    }
}

As such i'm getting a "Object reference not set to an instance of an object. - NullReferenceException". I'm not sure why this is happening because I can make modifications to a Student object from the MainWindow.

Edit: TeacherTools Class

public partial class TeacherTools : Window
{
    private List<Student> studentList = new List<Student>();

    public TeacherTools()
    {
        InitializeComponent();
    }

    public List<Student> Kids
    {
       get { return studentList; }
       //set { studentList = value; }
    }

    private void newStudentClick(object sender, RoutedEventArgs e)
    {
        Student student = new Student();
        student.Name = nameBox.Text;
        studentList.Add(student);
        studentData.ItemsSource = studentList;
        studentData.Items.Refresh();
        //nameBox
    }
}
4

3 回答 3

2

In your window named "Test" you are using the object teachTools but you haven't created this anywhere.

You do have a similarly named object in your MainWindow, but this is not shared with the Test-window. You should also create a new instance in the Test-window

于 2013-04-15T01:41:34.603 回答
1

Either studentList contains an item that is null (which would cause the s.Name part to throw), or studentName is null (which would cause the studentName.Text part to throw).

于 2013-04-15T02:49:08.673 回答
1

It appears that when you open the Test window you are creating a new instance of the TeacherTools window, which means there are no elements in the Kids list. If this is the way you wanted to accomplish this, you could set the TeacherTools instance in the Test window as a public property and pass the object from the main window, like so:

//Test Window
public partial class Test : Window
{
    Student loadedStudent = new Student();
    public TeacherTools teachTools { get; set; }
    ...
}

//Main Window
public partial class MainWindow : Window
{
    ...

    private void Selection_Click(object sender, RoutedEventArgs e)
    {
        this.Hide();
        myTest.teachTools = this.teachTools;
        myTest.ShowDialog();
        this.Show();
    }
}

Note that I haven't tested this, but I think this is the approach you are looking for. I will also say that you could be playing a dangerous game by passing the different windows between each other instead of using a class to handle this.

于 2013-04-15T03:39:17.927 回答