System.NullReferenceException基于List [duplicate]

时间:2021-10-13 16:54:10

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to loop through rows and get their Indexes (Primary Keys from SQL). I'm getting a NRE on "this.SelectRowIndexes.Add(ResourceKey)" I can't figure out why it would matter and how can I fix this?

我正在尝试循环遍历行并获取其索引(SQL中的主键)。我在“this.SelectRowIndexes.Add(ResourceKey)”上得到一个NRE我无法弄清楚为什么它会重要,我该如何解决这个问题?

CODE:

    private void GetIndexes()
    {
        List<int> SelectRowIndexes = new List<int>();
        for (int i = 0; i < gridViewRecords.Rows.Count; i++)
        {
            DataRowView drv = (DataRowView)gridViewRecords.CurrentRow.DataBoundItem;
            DataRow selectedRow = drv.Row;
            ResourceKey = Convert.ToInt32(selectedRow["ResourceAndInstallKey"]);
            this.SelectRowIndexes.Add(ResourceKey);

        }
    }

I also have it up in my class (this has been a ton of troubleshooting, so my code looks terrible)

我在课堂上也有它(这已经是大量的故障排除,所以我的代码看起来很糟糕)

    public List<int> SelectRowIndexes { get; set; }

I had this prior. Several of the answers quoted this code. I changed mine because the if-else was actually used for something else, which has now been deleted

我之前有这个。几个答案引用了这段代码。我改变了我的意思,因为if-else实际上用于其他东西,现在已被删除

if (this.SelectRowIndexes == null)
{
    this.SelectRowIndexes.Add(ResourceKey);
}
else
{
    this.SelectRowIndexes.Add(ResourceKey);
}

4 个解决方案

#1


What do you actually want to do if this.SelectRowIndexes is null? Currently you're just unconditionally calling Add on it, because both branches of your if statement do the same thing.

如果this.SelectRowIndexes为null,你真的想做什么?目前你只是无条件地调用Add,因为你的if语句的两个分支都做同样的事情。

Note that it definitely wouldn't be null if you'd assigned a new value to it - but instead in this line, you're declaring a new local variable called SelectRowIndexes:

请注意,如果您为其分配了一个新值,它肯定不会为null - 但在此行中,您将声明一个名为SelectRowIndexes的新局部变量:

List<int> SelectRowIndexes = new List<int>();

... which you're then completely ignoring. Perhaps you meant to set the value of the property/field instead?

......然后你完全无视了。也许您打算设置属性/字段的值?

SelectRowIndexes = new List<int>();

With that change, you should avoid the exception - but you'll still have the basically-broken code. You should almost certainly just get rid of the if check... it's not doing you any good right now.

有了这个改变,你应该避免异常 - 但你仍然会有基本上破坏的代码。你几乎可以肯定地摆脱if检查...现在对你没有好处。

However, I would suggest that you probably should be declaring a separate local variable for resourceKey - the fact that you're updating an instance variable in a loop is somewhat peculiar... as is the fact that you're not using your loop index at all... you're doing the same thing for each iteration of the loop, using the current row rather than row i. Is that deliberate?

但是,我建议您可能应该为resourceKey声明一个单独的局部变量 - 您在循环中更新实例变量的事实有点奇怪......因为您没有使用循环索引这一事实根本就是......你在循环的每次迭代中使用当前行而不是第i行做同样的事情。这是故意的吗?

Fundamentally, you might well want to start this code again... it looks like you might just want to use LINQ:

从根本上说,您可能希望再次启动此代码...看起来您可能只想使用LINQ:

private void GetIndexes()
{
    SelectRowIndexes = gridViewRecords.Rows
        .Cast<DataRowView>()
        .Select(drv => (int) drv.Row["ResourceAndInstallKey"])
        .ToList();
}

#2


If SelectRowIndexes is null then you can't add anything to the list. You first need to initialize an empty list with

如果SelectRowIndexes为null,则无法向列表中添加任何内容。首先需要使用初始化空列表

this.SelectRowIndexes = new List<int>();

#3


The this keyword allows you to access a member named SelectRowIndexes within the scope of the class. That member is probably not initialized. Remove the List<int> type declaration and it will be perfect.

this关键字允许您在类的范围内访问名为SelectRowIndexes的成员。该成员可能尚未初始化。删除List 类型声明,它将是完美的。

private List<int> SelectRowIndexes;
private void GetIndexes()
{
    SelectRowIndexes = new List<int>();
    for (int i = 0; i < gridViewRecords.Rows.Count; i++)
    {
        DataRowView drv = (DataRowView)gridViewRecords.CurrentRow.DataBoundItem;
        DataRow selectedRow = drv.Row;
        ResourceKey = Convert.ToInt32(selectedRow["ResourceAndInstallKey"]);
        if (this.SelectRowIndexes == null)
        {
            this.SelectRowIndexes.Add(ResourceKey);
        }
        else
        {
            this.SelectRowIndexes.Add(ResourceKey);
        }
    }
}

#4


You instantiate a local variable

您实例化一个局部变量

List<int> SelectRowIndexes = new List<int>();

and then you are adding to your class property/field this.SelectedRowIndexes which you are most likely not assigning anywhere and it is null and you get NRE.

然后你要添加你的类属性/字段this.SelectedRowIndexes你很可能不会在任何地方分配它并且它是null并且你得到NRE。

this.SelectRowIndexes.Add(ResourceKey);

Change this so

改变这一点

private void GetIndexes()
{
    this.SelectRowIndexes = new List<int>();
    for (int i = 0; i < gridViewRecords.Rows.Count; i++)
    {
        DataRowView drv = (DataRowView)gridViewRecords.CurrentRow.DataBoundItem;
        DataRow selectedRow = drv.Row;
        ResourceKey = Convert.ToInt32(selectedRow["ResourceAndInstallKey"]);
        this.SelectRowIndexes.Add(ResourceKey);
    }
}

#1


What do you actually want to do if this.SelectRowIndexes is null? Currently you're just unconditionally calling Add on it, because both branches of your if statement do the same thing.

如果this.SelectRowIndexes为null,你真的想做什么?目前你只是无条件地调用Add,因为你的if语句的两个分支都做同样的事情。

Note that it definitely wouldn't be null if you'd assigned a new value to it - but instead in this line, you're declaring a new local variable called SelectRowIndexes:

请注意,如果您为其分配了一个新值,它肯定不会为null - 但在此行中,您将声明一个名为SelectRowIndexes的新局部变量:

List<int> SelectRowIndexes = new List<int>();

... which you're then completely ignoring. Perhaps you meant to set the value of the property/field instead?

......然后你完全无视了。也许您打算设置属性/字段的值?

SelectRowIndexes = new List<int>();

With that change, you should avoid the exception - but you'll still have the basically-broken code. You should almost certainly just get rid of the if check... it's not doing you any good right now.

有了这个改变,你应该避免异常 - 但你仍然会有基本上破坏的代码。你几乎可以肯定地摆脱if检查...现在对你没有好处。

However, I would suggest that you probably should be declaring a separate local variable for resourceKey - the fact that you're updating an instance variable in a loop is somewhat peculiar... as is the fact that you're not using your loop index at all... you're doing the same thing for each iteration of the loop, using the current row rather than row i. Is that deliberate?

但是,我建议您可能应该为resourceKey声明一个单独的局部变量 - 您在循环中更新实例变量的事实有点奇怪......因为您没有使用循环索引这一事实根本就是......你在循环的每次迭代中使用当前行而不是第i行做同样的事情。这是故意的吗?

Fundamentally, you might well want to start this code again... it looks like you might just want to use LINQ:

从根本上说,您可能希望再次启动此代码...看起来您可能只想使用LINQ:

private void GetIndexes()
{
    SelectRowIndexes = gridViewRecords.Rows
        .Cast<DataRowView>()
        .Select(drv => (int) drv.Row["ResourceAndInstallKey"])
        .ToList();
}

#2


If SelectRowIndexes is null then you can't add anything to the list. You first need to initialize an empty list with

如果SelectRowIndexes为null,则无法向列表中添加任何内容。首先需要使用初始化空列表

this.SelectRowIndexes = new List<int>();

#3


The this keyword allows you to access a member named SelectRowIndexes within the scope of the class. That member is probably not initialized. Remove the List<int> type declaration and it will be perfect.

this关键字允许您在类的范围内访问名为SelectRowIndexes的成员。该成员可能尚未初始化。删除List 类型声明,它将是完美的。

private List<int> SelectRowIndexes;
private void GetIndexes()
{
    SelectRowIndexes = new List<int>();
    for (int i = 0; i < gridViewRecords.Rows.Count; i++)
    {
        DataRowView drv = (DataRowView)gridViewRecords.CurrentRow.DataBoundItem;
        DataRow selectedRow = drv.Row;
        ResourceKey = Convert.ToInt32(selectedRow["ResourceAndInstallKey"]);
        if (this.SelectRowIndexes == null)
        {
            this.SelectRowIndexes.Add(ResourceKey);
        }
        else
        {
            this.SelectRowIndexes.Add(ResourceKey);
        }
    }
}

#4


You instantiate a local variable

您实例化一个局部变量

List<int> SelectRowIndexes = new List<int>();

and then you are adding to your class property/field this.SelectedRowIndexes which you are most likely not assigning anywhere and it is null and you get NRE.

然后你要添加你的类属性/字段this.SelectedRowIndexes你很可能不会在任何地方分配它并且它是null并且你得到NRE。

this.SelectRowIndexes.Add(ResourceKey);

Change this so

改变这一点

private void GetIndexes()
{
    this.SelectRowIndexes = new List<int>();
    for (int i = 0; i < gridViewRecords.Rows.Count; i++)
    {
        DataRowView drv = (DataRowView)gridViewRecords.CurrentRow.DataBoundItem;
        DataRow selectedRow = drv.Row;
        ResourceKey = Convert.ToInt32(selectedRow["ResourceAndInstallKey"]);
        this.SelectRowIndexes.Add(ResourceKey);
    }
}