Solved

Object reference not set to an Instance

Posted on 2012-04-02
12
537 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
ID: 37794870
which line throws the error?
0
 
LVL 42

Expert Comment

by:sedgwick
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 42

Expert Comment

by:sedgwick
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:sedgwick
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:
sedgwick earned 500 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:sedgwick
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 57
bound data table problem 2 33
Access/Visual Basic Question 3 24
Retrieve Active Directory Groups a User belongs to in VB.NET 3 18
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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