[VB.NET]未将对象引用设置为对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18279187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:05:23  来源:igfitidea点击:

[VB.NET]Object reference not set to an instance of an object

vb.netdatagridview

提问by Zebriukas Dry?iukas

I'm trying to add value of column from selected row on my datagridview to Collection(but I get same error if I do it with List or Array)

我正在尝试将 datagridview 上选定行的列值添加到 Collection(但如果我使用 List 或 Array 执行此操作,则会出现相同的错误)

CODE:

代码:

Dim zdgv = MyDataGridView

    For a = 0 To zdgv.SelectedRows.Count - 1

        MsgBox(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
        Try
            MyCollection.Add(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox(ex.InnerException)
        End Try

    Next

ex.Message = Object reference not set to an instance of an object

ex.InnerException = empty

ex.InnerException.Message = Makes program crash, goes to code screen, highlights MsgBox(ex.InnerException)line, and gives error: Object reference not set to an instance of an object

ex.Message =未将对象引用设置为对象的实例

ex.InnerException =

ex.InnerException.Message = 使程序崩溃,转到代码屏幕,突出显示MsgBox(ex.InnerException)行,并给出错误:未将对象引用设置为对象的实例

ADDITIONAL INFO:Using QuickWatch on zdgv gives me all info. Using it on Rows after it(zdgv) says: 'Rows' is not declared. It may be inaccessible due to its protection level.

附加信息:在zdgv 上使用 QuickWatch 为我提供了所有信息。在 it(zdgv) 之后在 Rows 上使用它说:'Rows' 未声明。由于其保护级别,它可能无法访问。

P.S. Yes I've googled, but none problem was similar. Yes I've searched here but no info. I've tryed r/visualbasic too - nothing... I've even tryed search for c# related stuff with this error - nothing. :/

PS 是的,我用谷歌搜索过,但没有类似的问题。是的,我在这里搜索过,但没有信息。我也尝试过 r/visualbasic - 什么都没有......我什至尝试搜索与此错误相关的 c# 相关内容 - 什么都没有。:/

Thanks in advance.

提前致谢。

EDIT1:I've tryed make non-databound datagridview in new project, and add one value from it to collection - same error. I guess I should go google about "Setting Reference of Object to an Instance of an Object".

EDIT1:我已经尝试在新项目中创建非数据绑定 datagridview,并将其中的一个值添加到集合中 - 同样的错误。我想我应该去谷歌一下“将对象的引用设置为对象的实例”。

EDIT2:This one was fail - newbie mistake.

EDIT2:这是失败 - 新手错误。

EDIT3:using quickwatch on

EDIT3:使用 quickwatch

zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString 

it shows right value(correct one, without throwing errors) = "1".

它显示正确的值(正确的一个,没有抛出错误)=“1”。

采纳答案by Matt B-L

This code works like a charm on my side.

这段代码对我来说就像一个魅力。

Did you forget a New on your MyCollection?

您是否忘记了 MyCollection 上的 New?

Dim zdgv = MyDataGridView
Dim MyCollection As New Collection
For a = 0 To zdgv.SelectedRows.Count - 1

    MsgBox(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
    Try
        MyCollection.Add(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
    Catch ex As Exception
        MsgBox(ex.Message)
        If ex.InnerException IsNot Nothing Then
            MsgBox(ex.InnerException)
        End If
    End Try
Next

回答by Chris

At top of code - just below public class classname and above first sub I've this: Public XXXXX As Collection

在代码顶部 - 就在公共类类名下方和第一个子类上方我有这个:Public XXXXX As Collection

You don't' create an instance of collectionand then you try add some items to it.

您不会创建一个实例,collection然后尝试向其中添加一些项目。

It should be:

它应该是:

Public XXXXX As New Collection

Public XXXXX As New Collection

Or you need to create a new instance somewhere else before access it

或者您需要在其他地方创建一个新实例才能访问它

XXXXX = New Collection

XXXXX = New Collection

回答by Matt B-L

ex.InnerException is null, and you try to access is Message attribute. It's a normal behavior. You should try something like

ex.InnerException 为 null,而您尝试访问的是 Message 属性。这是正常的行为。你应该尝试像

        Try
            MyCollection.Add(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
        Catch ex As Exception
            MsgBox(ex.Message)
            If ex.InnerException IsNot Nothing Then
                 MsgBox(ex.InnerException)
            End if
        End Try

InnerException is not null only if a sub method threw an exception under it.

只有当子方法在其下抛出异常时,InnerException 才不为 null。

回答by Steev Ba

Best Solution I foundBasically the error is that your code is making use of a non-existing row.

我发现的最佳解决方案基本上错误是您的代码正在使用不存在的行。

You will merely need to turn to false the datagridview "AllowUserToAddRows" property. Then all your normal loops will work properly.

您只需要将 datagridview 的“AllowUserToAddRows”属性设为 false。然后你所有的正常循环都会正常工作。

Dim zdgv = DataGridView1
 For Each row As DataGridViewRow In zdgv.Rows
        ListBox2.Items.Add(row.Cells(1).Value.ToString) 
  Next

or

或者

for i as integer = 0 to datagridView1.rows.count - 2
'enter code here

Next

MK :)

MK :)