Link to home
Start Free TrialLog in
Avatar of Norman Maina
Norman MainaFlag for Kenya

asked on

Object reference not set to an Instance

Hello All,
Am getting the Object reference not set to an instance error message when I load my form contaning a datagridview whenever the grid has null values.

How do i resolve that?

I have put default fro null as 0 in the datagridview formating.

 Private Function TotalGrossWgt() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
        If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
            For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
                tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn21").Value)
            Next i
        End If
        Return tot
    End Function
    Private Function TotalNetWgt() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
        If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
            For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
                tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn20").Value)
            Next i
        End If
        Return tot
    End Function
    Private Function NetWgt() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
        If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
            For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
                tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn19").Value)
            Next i
        End If
        Return tot
    End Function
    Private Function TotalQty() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
        If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
            For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
                tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn18").Value)
            Next i
        End If
        Return tot
    End Function

Open in new window

 Try
            TxtTotalQty.Text = 0
            TxtNetWgt.Text = 0
            TxtTotalNetWgt.Text = 0
            TxtGrossweight.Text = 0
            If Not (GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow) And GardenInvoiceDtlDataGridView.Rows.Count > 0 Then
                TxtTotalQty.Text = TotalQty().ToString("N")
                TxtNetWgt.Text = NetWgt().ToString("N")
                TxtTotalNetWgt.Text = TotalNetWgt().ToString("N")
                TxtGrossweight.Text = TotalGrossWgt().ToString("N")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Tea Tracing| GridFooter Changing", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Open in new window

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

which line throws the error?
try debug your code and when reach the line that throws the exception, add a validation check to make sure that it's not null.
Is it possible that GardenInvoiceDtlDataGridView.CurrentRow is nothing, even if GardenInvoiceDtlDataGridView is not Nothing?
yes it is possible, u need to check if GardenInvoiceDtlDataGridView.CurrentRow is not null first.
Avatar of Norman Maina

ASKER

Error is Thrown here:
If Not (GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow) And GardenInvoiceDtlDataGridView.Rows.Count > 0 Then
    ....

sedgwick>let em check as per your suggestion -Thanls very much for the quick response.Will get back to you.
sedgwick>I tried
If not  GardenInvoiceDtlDataGridView.Currencell.value is nothing

Open in new window


Not working
try:

if GardenInvoiceDtlDataGridView.Currencell is not nothing then
I have put this code on the paint event of the datagridview -it works fine until if finds rows with no values thats when it brings teh object reference not set to an instance error.

Private Function TotalQty() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
        If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
            For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
                tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn18").Value)
            Next i
        End If
        Return tot
    End Function

Open in new window

>Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow

That is the issue still. CurrentRow is null and you are trying to access it.

Why do you need to test that when you are just looping through all the rows?

If you do need that then use another if condition

If(Not IsNothing(GardenInvoiceDtlDataGridView.CurrentRow))
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can also use foreach as well:

For Each row As var In GardenInvoiceDtlDataGridView.Rows.Cast(Of DataGridViewRow)()
	tot += row.Cells("DataGridViewTextBoxColumn18").Value
Next

Open in new window


or linq:
tot = GardenInvoiceDtlDataGridView.Rows.Cast(Of DataGridViewRow)().Sum(Function(n) n.Cells("DataGridViewTextBoxColumn18").Value)

Open in new window

Sorry for late response -it worked like a charm.

Imagine i had commented the exception message to hide the problem.Now my code is fine,thanks to you.