Solved

Object reference not set to an Instance

Posted on 2012-04-02
12
525 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:sedgwick
Comment Utility
which line throws the error?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
Is it possible that GardenInvoiceDtlDataGridView.CurrentRow is nothing, even if GardenInvoiceDtlDataGridView is not Nothing?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
yes it is possible, u need to check if GardenInvoiceDtlDataGridView.CurrentRow is not null first.
0
 
LVL 12

Author Comment

by:NormanMaina
Comment Utility
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
Comment Utility
sedgwick>I tried
If not  GardenInvoiceDtlDataGridView.Currencell.value is nothing

Open in new window


Not working
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
try:

if GardenInvoiceDtlDataGridView.Currencell is not nothing then
0
 
LVL 12

Author Comment

by:NormanMaina
Comment Utility
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
Comment Utility
>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:
sedgwick earned 500 total points
Comment Utility
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:sedgwick
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now