-1

I need help for this problem and i have no idea on how to solve this.

This is the code;

Private Sub GetLabandOtherFees()
        Dim dic As New Dictionary(Of String, Integer)()
        Dim cellValue As String = Nothing
        For i As Integer = 0 To dgvSubjectsEnrolled.Rows.Count - 1
            If Not dgvSubjectsEnrolled.Rows(i).IsNewRow Then
                cellValue = dgvSubjectsEnrolled(0, i).Value.ToString()
                If Not dic.ContainsKey(cellValue) Then
                    dic.Add(cellValue, 1)
                Else
                    dic(cellValue) += 1
                End If
            End If
        Next

        Dim sb As New StringBuilder()
        For Each keyvalue As KeyValuePair(Of String, Integer) In dic
            sb.AppendLine(String.Format("{0}", keyvalue.Key, keyvalue.Value))
        Next

        For Each row As DataGridViewRow In dgvsub.Rows
            row.Cells(0).Value = sb.ToString()
            row.Cells(1).Value = dic.Count.ToString()
        Next
    End Sub

And this is the error;

"Object Reference Not set to an instance of an object." 
Check to determine if object is null before calling the method.

This error is putting headache on me. please help.

deitysha
  • 21
  • 8

1 Answers1

0

Read what was in the error. Here it is, try this one, just add a little bit of conditions in it.

               If dgvSubjectsEnrolled(0, i).Value > 0 Then
                    cellValue = dgvSubjectsEnrolled(0, i).Value.ToString()
                    If Not dic.ContainsKey(cellValue) Then
                        dic.Add(cellValue, 1)
                    Else
                        dic(cellValue) += 1
                    End If
                Else
                End If