title: Object reference not set to an instance of an object. vb.net looking up a string
body: <p>I'm getting this error and I don't know how to fix it. I know i'm supposed to write what I've tried, but i have no idea what to try, even after looking for a few hours about how to deal with the error.</p>
  
  <p>If more information is needed please let me know. Thanks</p>
  
  <p>The highlighted part of the code is:</p>
  
  <pre><code>Dim cdtrabajador As String = dgvr.Cells(0).Value.ToString
  </code></pre>
  
  <p>Object reference not set to an instance of an object.</p>
  
  <p>NullReference Exception was unhandled by user code</p>
  
  <pre><code>Private Sub PeopleDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
      If DataGridView1.RowCount &gt; 0 AndAlso e.RowIndex &gt; -1 Then
          If e.RowIndex &gt; -1 Then
              Dim dgvr As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
              Dim cdtrabajador As String = dgvr.Cells(0).Value.ToString
              Dim qry = From dr As PersonalObraDataSet.PersonalObRow In PersonalObraDataSet.PersonalOb Where dr.cdTrabajador = cdtrabajador
              If qry.Count &gt; 0 Then
                  Dim Nombre As String = qry.First.Nombre1
                  dgvr.Cells(1).Value = Nombre
              End If
          End If
      End If
  End Sub
  </code></pre>