1

Whenever I run the following code it shows the error as "object reference not set to an instance of an object" : (This codes change the value of DatagridViewComboBox as per other DatagridViewComboBox in same row and sharing same databse table.)

Private Sub dgv1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellValueChanged

    Try

        Dim currentrowindex As Integer = dgv1.CurrentRow.Index
        Dim obj As Object = dgv1.CurrentCell.Value           
        Me.dgv1(1, currentrowindex).Value = obj
        Me.dgv1(2, currentrowindex).Value = obj
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub



Private Sub dgv1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.CurrentCellDirtyStateChanged
   dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit) 

End Sub

Please tell me how can I fix this error ???

  • For the possible reasons of a NullReferenceException, see this post: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. That said, I suggest to run your application in Debug mode and check if any of your object references are `Nothing`. A candidate is `dgv1.CurrentRow`, another `dgv1.CurrentCell`. – Markus Mar 08 '14 at 06:45
  • Use a debugger to find the value which is Null. I suspect dgv1 is not initialized. – PMF Mar 08 '14 at 06:46
  • thanks I've done this: Private Sub datagridview2_cellvaluechanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Try Dim currentrowindex As Integer = dgv2.CurrentRow.Index Dim obj As Object = dgv2.CurrentCell.Value ' we can take STRING or OBJECT var is mandatory Me.dgv2(4, currentrowindex).Value = obj Me.dgv2(5, currentrowindex).Value = obj Catch ex As Exception MsgBox(ex.Message) End Try End Sub handler in form load: AddHandler dgv2.CellValueChanged,AddressOf datagridview2_cellvaluechanged – user3364608 Mar 09 '14 at 09:59

1 Answers1

0

thanks to everyone now I have done this as : Private Sub datagridview2_cellvaluechanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

    Try
        Dim currentrowindex As Integer = dgv2.CurrentRow.Index
        Dim obj As Object = dgv2.CurrentCell.Value   ' we can take STRING or OBJECT var is mandatory

        Me.dgv2(4, currentrowindex).Value = obj
        Me.dgv2(5, currentrowindex).Value = obj
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

and add a handler in form load : AddHandler dgv2.CellValueChanged, AddressOf datagridview2_cellvaluechanged