Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27417

Object reference not set to an instance of an object

$
0
0
I keep getting this error and I don't know what causes this error or how to fix it, can someone please help me out?

Code:

Private Function getCoordinates()
        Dim dt As New DataTable
        Dim dt2 As New DataTable
        Dim r As DataRow
        Dim n As Integer
        Dim selectedItems As DataGridViewSelectedRowCollection = dgv.SelectedRows
        dt = dgv.DataSource
        dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dgv.MultiSelect = True
        Dim x, y As Integer





        dt2.Columns.Add("Command")
        dt2.Columns.Add("Position")

        Try

            If selectedItems Is Nothing Then
                For n = 0 To dt.Rows.Count - 1
                    r("Command") = dt.Rows.Item(n)("Mouse Command")
                    r("Position") = dt.Rows.Item(n)("Mouse Position")
                    dt2.Rows.Add(r)
                Next

            Else

             

                For Each selectedItem As DataGridViewRow In selectedItems
                    Dim index = Convert.ToString(selectedItem.Cells(0).Value)
                    r("Command") = index
                    r("Position") = dt.Rows(selectedItem.Cells(1).Value.ToString)("Mouse Position")
                    dt2.Rows.Add(r)

                Next
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

I get an error at the line
Code:

r("Command") = index
This line returns a string value

Viewing all articles
Browse latest Browse all 27417

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>