troubleshooting Question

System.NullReferenceException: Object reference not set to an instance of an object.

Avatar of Tim313
Tim313Flag for United States of America asked on
.NET ProgrammingVisual Basic.NET
5 Comments2 Solutions561 ViewsLast Modified:
I've been running this code for a few months and have just recently began to get this error message occasionally:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Main.frmScheduleL2.TblSched2DataGridView_CurrentCellChanged(Object sender, EventArgs e)

Sometimes the form loads, sometimes not and the error message is shown. Can anyone help?
The CurrentCellChanged event below:

Private Sub TblSched2DataGridView_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.CurrentCellChanged
        SaveMessage = True

        If dgv1.CurrentRow.Cells(8).Selected = True Then
            If IsDBNull(dgv1.CurrentRow.Cells(4)) Or dgv1.CurrentRow.Cells(4).FormattedValue.ToString = "" Then
                Exit Sub
            Else
                varProduct1 = dgv1.CurrentRow.Cells(4).FormattedValue.ToString
                varWidth1 = CDbl(dgv1.CurrentRow.Cells(6).Value.ToString)
                varLength1 = CInt(dgv1.CurrentRow.Cells(7).Value.ToString)

                Dim dbconnFNoA As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\Process1\db\Main Application.mdb")
                Dim qryFNoA As String
                Dim calcFNoA As Integer
                qryFNoA = "SELECT COUNT(FiltrairNo) FROM tblFiltrairNo WHERE (Product = '" & varProduct1 & "' AND Width = " & varWidth1 & " AND Length = " & varLength1 & " AND RollsBag = " & 0 & ")"
                Dim cmdFNoA As New OleDb.OleDbCommand(qryFNoA, dbconnFNoA)
                dbconnFNoA.Open()
                calcFNoA = cmdFNoA.ExecuteScalar()
                dbconnFNoA.Close()

                If calcFNoA = 0 Then
                    dgv1.CurrentRow.Cells(6).Style.BackColor = Color.Yellow
                    dgv1.CurrentRow.Cells(7).Style.BackColor = Color.Yellow
                Else
                    dgv1.CurrentRow.Cells(6).Style.BackColor = Color.White
                    dgv1.CurrentRow.Cells(7).Style.BackColor = Color.White
                End If
            End If
        End If

        If dgv1.CurrentRow.Cells(17).Selected = True Then
            If IsDBNull(dgv1.CurrentRow.Cells(13)) Or dgv1.CurrentRow.Cells(13).FormattedValue.ToString = "" Then
                Exit Sub
            Else
                varProduct2 = dgv1.CurrentRow.Cells(13).FormattedValue.ToString
                varWidth2 = CDbl(dgv1.CurrentRow.Cells(15).Value.ToString)
                varLength2 = CInt(dgv1.CurrentRow.Cells(16).Value.ToString)

                Dim dbconnFNoB As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\Process1\db\Main Application.mdb")
                Dim qryFNoB As String
                Dim calcFNoB As Integer
                qryFNoB = "SELECT COUNT(FiltrairNo) FROM tblFiltrairNo WHERE (Product = '" & varProduct2 & "' AND Width = " & varWidth2 & " AND Length = " & varLength2 & " AND RollsBag = " & 0 & ")"
                Dim cmdFNoB As New OleDb.OleDbCommand(qryFNoB, dbconnFNoB)
                dbconnFNoB.Open()
                calcFNoB = cmdFNoB.ExecuteScalar()
                dbconnFNoB.Close()

                If calcFNoB = 0 Then
                    dgv1.CurrentRow.Cells(15).Style.BackColor = Color.Yellow
                    dgv1.CurrentRow.Cells(16).Style.BackColor = Color.Yellow
                Else
                    dgv1.CurrentRow.Cells(15).Style.BackColor = Color.White
                    dgv1.CurrentRow.Cells(16).Style.BackColor = Color.White
                End If
            End If
        End If

    End Sub
ASKER CERTIFIED SOLUTION
volking

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros