Answered DataRow System.NullReferenceException

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
Hi,

I have a MyDataGridView.DataSource = MyDataSet.Table(0) and the AllowUserToAddRows = True
Once the user have finished typing in the column zero I have to check if they didn't type invalid stuff. It have to be numeric and 4 chars long.

the code is:
VB.NET:
    Private Sub Result_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Result.CellEndEdit
        If e.ColumnIndex = 0 Then
            If Not (IsNumeric(Result.Rows(e.RowIndex).Cells(0).Value) And Result.Rows(e.RowIndex).Cells(0).Value.ToString.Length = 4) Then
                Result.Rows(e.RowIndex).Cells(0).Value = ""
                MessageBox.Show("User ID must be a number with 4 digits", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If

        Me.LockBtn.ImageIndex = 2
        CleanUp.Enabled = False
    End Sub

And it is not all the time, but depending on how quick and stupid (i'm expecting stupid users, as usual) I click on the previous row or the AddNewRow row I get a the damn System.NullReferenceException on my If line:
VB.NET:
  If Not (IsNumeric(Result.Rows(e.RowIndex).Cells(0).Value) And Result.Rows(e.RowIndex).Cells(0).Value.ToString.Length = 4) Then

I'm really not in the mood of just put a Try Catch, as it looks like bad programming in this case. I've already tried a couple variations with "is nothing" but no success

So my question:
Why is this happening?
How could test it won't cause a Null expection before testing the row value?
 
Last edited:
Kinda weird that on CellEndEdit the row still not defined, but I agree with you, validating is for validation, and now it works smooth.
Thanks.

VB.NET:
    Private Sub Result_Validating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles Result.CellValidating
        If e.ColumnIndex = 0 Then
            If e.FormattedValue = "" Then Exit Sub
            If Not (IsNumeric(e.FormattedValue) And (e.FormattedValue.ToString.Length = 4)) Then
                MessageBox.Show("User ID must be a number with 4 digits", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                e.Cancel = True
            End If
        End If
    End Sub
 
If Not (IsNumeric(Result.Rows(e.RowIndex).Cells(0).Value) And Result.Rows(e.RowIndex).Cells(0).Value.ToString.Length = 4) Then
Theoretically, if .Value is Nothing here then calling instance member .ToString will throw NullReference exception. Things like this happens often and solving it may be checking for Nothing first, often combined with short-circuiting operators like AndAlso/OrElse to conditionally test the value, or perhaps using the CStr function instead which does not throw on Nothing.
 
hmmm.... interesting, I though the exception was happening on accessing the DataRow, not the value.ToString, maybe was both.
But the solution using the .CellValidating event is definitely more adequate, specially cause now I can use e.cancel = True to hold the focus on the cell and make the user input a correct value.

Thanks guys.
 
Back
Top