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

5
  • 1
    One of the objects or properties you are referencing is null. Do you know on which line the error occurs? Commented Aug 16, 2013 at 17:45
  • 1
    @valverij Do you know on which line the error occurs? Probably the single line between Try and Catch ;-) Commented Aug 16, 2013 at 17:55
  • 1
    possible duplicate of What is a NullReferenceException in .NET and how do I fix it? Commented Aug 16, 2013 at 17:55
  • 1
    What is MyCollection? - did you forget to make a New on it? Commented Aug 16, 2013 at 18:13
  • At top of code - just below public class classname and above first sub I've this: Public eilutesnumeriukas As Collection Commented Aug 16, 2013 at 18:15

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

4 Comments

THANKS. It worked. THANK YOU SO MUCH. My city is atm one of most popular among tourists in Lithuania - it's Telsiai. If you ever come here - I get you a beer. Like seriously.
@ZebriukasDryžiukas I shall remember that :)
Are you inviting only Matt?
FREE BEER FOR EVERYONE! And as we know Russians are superior hackers, and we also know they love vodka. So I can assume Beer could improve programming skills, right?
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.

6 Comments

CSharp Basic .NET? :D null in VB.NET is Nothing and != is <>
By the way, maybe I'm wrong, but it'd be If Not ex.InnerException Is Nothing Then...
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.
@ZebriukasDryžiukas if your initial problem is not about ex.InnerException, please tell debug and give us more informations about the breaking part
@MatíasFidemraizer If Not (...) Is Nothing Or If (...) IsNot Nothing is excatly the same in VB.Net
|
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

1 Comment

Yes @Matt Already answered it - still thanks. P.S. I was using MyCollection name in this thread to cause less confusion with unknown words.
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.