4

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)

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

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.

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. :/

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".

EDIT2: This one was fail - newbie mistake.

EDIT3: using quickwatch on

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

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

likeitlikeit
  • 5,317
  • 4
  • 33
  • 53
  • 1
    One of the objects or properties you are referencing is `null`. Do you know on which line the error occurs? – valverij Aug 16 '13 at 17:45
  • 1
    @valverij *Do you know on which line the error occurs?* Probably the single line between `Try` and `Catch` ;-) – Chris Aug 16 '13 at 17:55
  • 1
    possible duplicate of [What is a NullReferenceException in .NET and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net-and-how-do-i-fix-it) – asawyer Aug 16 '13 at 17:55
  • 1
    What is MyCollection? - did you forget to make a New on it? – Matt B-L Aug 16 '13 at 18:13
  • At top of code - just below `public class classname` and above first sub I've this: `Public eilutesnumeriukas As Collection` – Zebriukas Dryžiukas Aug 16 '13 at 18:15

4 Answers4

3

This code works like a charm on my side.

Did you forget a New on 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
Matt B-L
  • 253
  • 1
  • 8
1

ex.InnerException is null, and you try to access is Message attribute. It's a 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 a sub method threw an exception under it.

Matt B-L
  • 253
  • 1
  • 8
  • 2
    CSharp Basic .NET? :D `null` in VB.NET is `Nothing` and `!=` is `<>` – Matías Fidemraizer Aug 16 '13 at 17:46
  • 1
    By the way, maybe I'm wrong, but it'd be `If Not ex.InnerException Is Nothing Then`... – Matías Fidemraizer Aug 16 '13 at 17:47
  • I was using that to try find error, but still thanks to both. At the moment trying to do this on new project with simple datagridview. Gonna see if error repeats. – Zebriukas Dryžiukas Aug 16 '13 at 17:48
  • 1
    @ZebriukasDryžiukas if your initial problem is not about ex.InnerException, please tell debug and give us more informations about the breaking part – Matt B-L Aug 16 '13 at 17:57
  • 1
    @MatíasFidemraizer `If Not (...) Is Nothing` Or `If (...) IsNot Nothing` is excatly the same in `VB.Net ` – Chris Aug 16 '13 at 18:00
  • @Matt - I just want to insert value of cell from datagridview to Collection. Using code I've shown in posts gives _Object reference not set to an instance of an object_ error. I've used `Try Catch` just to make it easier debugging. – Zebriukas Dryžiukas Aug 16 '13 at 18:05
1

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

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

It should be:

Public XXXXX As New Collection

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

XXXXX = New Collection

Chris
  • 7,989
  • 10
  • 30
  • 48
0

Best Solution I found Basically, 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.

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 :)

Osama Rizwan
  • 601
  • 1
  • 4
  • 18