Solved

Object reference not set to an Instance

Posted on 2012-04-02
12
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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.

 
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

615 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