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

I am trying to add a column value from a selected row in my datagridview to Collection (but I get the same error if I do this with a list or 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 object instance

ex.InnerException = empty

ex.InnerException.Message = Makes a program error, goes to the code screen, selects the line MsgBox (ex.InnerException) and gives an error: Object link is not set to the object instance

FURTHER INFORMATION: Using QuickWatch on zdgv gives me all the info. Using it on the lines after it (zdgv) says: "Lines" are not declared. It may not be available due to its level of protection.

PS Yes, I googled, but not a single problem was alike. Yes, I searched here, but no information. I also tried r / visualbasic - nothing ... I even tried searching for C # related with this error - nothing.: /

Thanks in advance.

EDIT1: I tried to make a datagridview without data binding in a new project and add one value from it to the collection - same error. I think I should google about "Setting up an object reference to an object instance".

EDIT2: This error was a bug - newbie.

EDIT3: using quick view

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

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

+4
source share
3 answers

This code works like a charm on my side.

Have you forgotten the new in your MyCollection?

 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 
+3
source

ex.InnerException is null and you are trying to access the Message attribute. This is normal behavior. You should try something like

  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 the routine throws an exception to it.

+1
source

At the top of the code is just below the public class classname and above the first sub I have: Public XXXXX As Collection

You do not create an instance of collection , and then try to add some elements to it.

It should be:

Public XXXXX As New Collection

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

XXXXX = New Collection

+1
source

Source: https://habr.com/ru/post/1497478/


All Articles