Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Object reference not set to an Instance

Posted on 2012-04-02
12
Medium Priority
?
571 Views
Last Modified: 2012-04-05
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

0
Comment
Question by:NormanMaina
12 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 37794870
which line throws the error?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 37794873
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.
0
 
LVL 11

Expert Comment

by:Visio_Guy
ID: 37795030
Is it possible that GardenInvoiceDtlDataGridView.CurrentRow is nothing, even if GardenInvoiceDtlDataGridView is not Nothing?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 37795264
yes it is possible, u need to check if GardenInvoiceDtlDataGridView.CurrentRow is not null first.
0
 
LVL 12

Author Comment

by:NormanMaina
ID: 37795334
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.
0
 
LVL 12

Author Comment

by:NormanMaina
ID: 37796352
sedgwick>I tried
If not  GardenInvoiceDtlDataGridView.Currencell.value is nothing

Open in new window


Not working
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 37796370
try:

if GardenInvoiceDtlDataGridView.Currencell is not nothing then
0
 
LVL 12

Author Comment

by:NormanMaina
ID: 37796393
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

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37797085
>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))
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 37799500
you didnt check GardenInvoiceDtlDataGridView.CurrentRow if not null:
Private Function TotalQty() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
If Not  GardenInvoiceDtlDataGridView.CurrentRow Is Nothing Then
        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
End If
        Return tot
    End Function
                                            

Open in new window

0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 37799515
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

0
 
LVL 12

Author Closing Comment

by:NormanMaina
ID: 37809737
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.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question