C# 为什么这段代码在从文本文件向listbox添加字符串时抛出NullReferenceException?

C# 为什么这段代码在从文本文件向listbox添加字符串时抛出NullReferenceException?,c#,visual-studio-2010,text,listbox,C#,Visual Studio 2010,Text,Listbox,代码如下: public partial class Form1 : Form { //Initialize Array to hold student names new string[] names = { "David Palladini", "Michael Reyes", "Bram Lesser", "Hans Herrmann", "Nathan Texeira" }; //Initialize Array to hold student evaluati

代码如下:

public partial class Form1 : Form
{
    //Initialize Array to hold student names
    new string[] names = { "David Palladini", "Michael Reyes", "Bram Lesser", "Hans Herrmann", "Nathan Texeira" };

    //Initialize Array to hold student evaluations
    new double[,] evaluation = { {1.0, 0.8, 0.9 ,1.0,0.6},
                                 {0.2, 0.9, 0.5, 0.6, 0.7},
                                 {0.5, 1.0, 1.0, 1.5, 0.9},
                                 {0.9, 0.9, 0.9, 1.0, 1.0},
                                 {1.0, 0.9, 1.0, 0.8, 0.9} };


    public Form1()
    {
        StreamWriter outputFile;
        outputFile = File.CreateText("names.txt");

        foreach (string name in names)
        {
            outputFile.WriteLine(name);
        }

        outputFile.Close();

        string studentName;
        StreamReader inputFile;
        inputFile = File.OpenText("names.txt");

        while (!inputFile.EndOfStream)
        {
            //Reads name from text file
            studentName = inputFile.ReadLine();

            //Writes name to listbox
            nameListBox.Items.Add(studentName);
        }

        inputFile.Close();

        InitializeComponent();
    }
评估数组暂时不相关。我试图在启动时立即将名称数组中的所有名称显示到列表框中。我还知道,将它们写入文本文件,然后从中读取它们是一种非常迂回的方式,但在这种情况下,我不得不这样做

问题在于,它在此处抛出NullReferenceException错误:

        //Writes name to listbox
        nameListBox.Items.Add(studentName);

为了我的生命,我不知道为什么。原始数组是否未正确写入文本文件?还是我在试图读回字符串时做错了什么?

我认为在调用
InitializeComponent()之前,
nameListBox
不会存在
尝试将该行移到Form1构造函数的顶部。

我认为在调用
InitializeComponent()之前,
名称列表框
不会存在尝试将该行移到Form1构造函数的顶部。

尝试将初始化移到顶部:

public Form1()
{
    InitializeComponent();
    StreamWriter outputFile;
    outputFile = File.CreateText("names.txt");

    foreach (string name in names)
    {
        outputFile.WriteLine(name);
    }

    outputFile.Close();

    string studentName;
    StreamReader inputFile;
    inputFile = File.OpenText("names.txt");

    while (!inputFile.EndOfStream)
    {
        //Reads name from text file
        studentName = inputFile.ReadLine();

        //Writes name to listbox
        nameListBox.Items.Add(studentName);
    }

    inputFile.Close();


}

尝试将初始化移动到顶部:

public Form1()
{
    InitializeComponent();
    StreamWriter outputFile;
    outputFile = File.CreateText("names.txt");

    foreach (string name in names)
    {
        outputFile.WriteLine(name);
    }

    outputFile.Close();

    string studentName;
    StreamReader inputFile;
    inputFile = File.OpenText("names.txt");

    while (!inputFile.EndOfStream)
    {
        //Reads name from text file
        studentName = inputFile.ReadLine();

        //Writes name to listbox
        nameListBox.Items.Add(studentName);
    }

    inputFile.Close();


}

确保
nameListBox
存在并已初始化,确保
studentName
存在并包含数据。确保
nameListBox存在并已初始化,确保
studentName
存在并包含数据。哇,这是一个简单的修复方法。非常感谢。你能解释一下为什么会这样吗?我的理解是,我可以编写启动表单的条件,然后{InitializeComponent():}将在这些约束下初始化它。我猜它在技术上仍然可以做到这一点,但我必须首先使用该方法?当您在IDE(Visual Studio等)中构建表单时,通过调用
InitializeComponent()生成用于构建和配置这些对象的代码方法。在调用initialize方法之前,您仍然可以在intellisense中看到您的对象,因为它们已经声明,但是您无法使用它们,因为它们没有被赋值。哇,这是一个很容易解决的问题。非常感谢。你能解释一下为什么会这样吗?我的理解是,我可以编写启动表单的条件,然后{InitializeComponent():}将在这些约束下初始化它。我猜它在技术上仍然可以做到这一点,但我必须首先使用该方法?当您在IDE(Visual Studio等)中构建表单时,通过调用
InitializeComponent()生成用于构建和配置这些对象的代码方法。在调用initialize方法之前,您仍然可以在intellisense中查看对象,因为它们已声明,但您无法使用它们,因为它们尚未被赋值。