简体   繁体   English

由于此错误,我的代码行无法工作?

[英]My code line cannot work because of this error?

So basically here is the problem, I have tried many ways to fix it but none of them worked !所以基本上问题就在这里,我尝试了很多方法来解决它,但没有一个奏效! Error is getting at this line : Templist.Add((string)result["titre"]; . Templist is the error !错误出现在这一行: Templist.Add((string)result["titre"]; Templist是错误!

private List<string> ReadNews()
{
    string SqlText = "SELECT * FROM nom_table"; 
    List<string> Templist; 
    MySqlConnection SqlConnection = new 
    MySqlConnection(TheConnectionString);
    MySqlCommand SqlCommand = new 
    MySqlCommand(SqlText,SqlConnection);


    MySqlDataReader result = SqlCommand.ExecuteReader();

    while (result.Read())
    {
        Templist.Add((string)result["titre"]);
    }

    return Templist;
}

Because you haven't initialized the Templist , until you initialize/assign an instance the declared variable( Templist ) will be null, and you are not able to add something to null that is what the error message says.因为您还没有初始化Templist ,所以在您初始化/分配一个实例之前,声明的变量( Templist )将为空,并且您无法向null添加一些内容,这就是错误消息所说的。 The code should be :代码应该是:

List<string> Templist =  new List<string>();

One more thing I would like to add here is the usage of using for proper disposal of the objects, and also specified column names instead for * .我想在这里添加的另一件事是使用using正确处理对象,并指定column names而不是* Consider the modified snippets below:考虑以下修改后的片段:

private List<string> ReadNews()
{
    string SqlText = "SELECT titre FROM nom_table";
    List<string> Templist = new List<string>();
    using (SqlConnection SqlConnection = new MySqlConnection(TheConnectionString))
    {
        using (MySqlCommand SqlCommand = new MySqlCommand(SqlText, SqlConnection))
        {
            using (MySqlDataReader result = SqlCommand.ExecuteReader())
            {
                while (result.Read())
                {
                    Templist.Add((string)result["titre"]);
                }
            }

        }
    }
    return Templist;
}

Your Templist should be initialized before use.您的Templist应在使用前初始化。 Change your declaration line to this:将您的声明行更改为:

List<string> Templist = new List<string>();

Templist is null, you haven't created an instance yet. Templist为空,您尚未创建实例。 Initialize:初始化:

List<string> Templist = new List<string>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于错误 CS0111,无法正确编写我的代码 - Cannot write my code right, because of an error CS0111 我的代码只能逐行执行 - My code work only on line by line executing 无法修改控件集合,因为控件包含代码块错误 - The Controls collection cannot be modified because the control contains code blocks Error 由于错误,无法运行我的NUnit测试 - Cannot get my NUnit tests to run because of error 无法在本地运行master,因为我的EF代码优先迁移不同步 - Cannot run master locally because my EF Code First Migrations out of sync 我的数据库无法在 VS Code 中打开,该文件未显示在编辑器中,因为它是二进制文件 - My database cannot be opened in VS Code, the file is not displayed in the editor because it is either binary 我应该在下面的代码中的哪一行提交我的工作单元? - At which line in the following code should I commit my unit of work? 我的紧急停止按钮不起作用,因为代码正在运行并且 UI 没有响应 - My Emergency Stop button doesn't work because the code is running and the UI won't respond 无法修改控件集合,因为控件包含代码块(即&lt;%…%&gt;)错误,无法正常工作 - The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>) error not working 无法修改控件集合,因为控件包含代码块(即&lt;%…%&gt;)错误 - The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM