NullReferenceException

Hello,

I am getting an error message when i show a new form and try to execute the following onload code.  The program errors out @ the declarations.  Thanks for any help.

Here is the code.

Imports System.Data.SqlServerCe
Imports System.IO
Public Class Totals
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
'removed
#End Region
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Dim cn As SqlCeConnection
        Dim sqlEngine As SqlCeEngine
        Dim cmd As SqlCeCommand = cn.CreateCommand
        Dim da As New SqlCeDataAdapter

        Closeme.Enabled = False
        cn = New SqlCeConnection("Data Source=\My Documents\HandheldDBWeight.sdf")
        cn.Open()
        Dim NumberOfRecords As Integer

        cmd.CommandText = "SELECT Count(""Wing Band Number"") FROM TemporaryWeight WHERE ""Weight"" Is Null"
        cmd.Connection = cn
        dr = cmd.ExecuteReader
        dr.Read()
        NumberOfRecords = dr.GetValue(0)
        Incomplete.Text = NumberOfRecords

        cmd.CommandText = "SELECT Count(""Wing Band Number"") FROM TemporaryWeight WHERE ""Weight"" Is Not Null"
        cmd.Connection = cn
        dr = cmd.ExecuteReader
        dr.Read()
        NumberOfRecords = dr.GetValue(0)
        complete.Text = NumberOfRecords

        Dim table As New DataTable
        Dim SQL As String
        SQL = "SELECT ""Cage Number"", ""Wing Band Number"", Feed, Weight FROM TemporaryWeight ORDER BY ""Cage Number"""

        cmd.CommandText = SQL
        cmd.Connection = cn
        da.SelectCommand = cmd
        da.Fill(table)
        MyGrid.DataSource = table

        If cn.State <> ConnectionState.Closed Then
            cn.Close()
        End If

        MyGrid.ColumnHeadersVisible = False
        Closeme.Enabled = True
    End Sub
    Private Sub Quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Closeme.Click
        Me.Close()
    End Sub
End Class

Error Message...

An unhandled exception of type 'System.NullReferenceException' occurred in (Project Name).exe
LVL 7
NavicertsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

appariCommented:
you are trying to access cn object before its instance is created.
try shifting the declaration of "cmd" to after "cn=new ..." like this

        Dim cn As SqlCeConnection
        Dim sqlEngine As SqlCeEngine
        'Dim cmd As SqlCeCommand = cn.CreateCommand
        Dim da As New SqlCeDataAdapter

        Closeme.Enabled = False
        cn = New SqlCeConnection("Data Source=\My Documents\HandheldDBWeight.sdf")
Dim cmd As SqlCeCommand = cn.CreateCommand
        cn.Open()
        Dim NumberOfRecords As Integer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
appariCommented:
or like this

Dim cn As SqlCeConnection
        Dim sqlEngine As SqlCeEngine
        Dim cmd As SqlCeCommand '= cn.CreateCommand
        Dim da As New SqlCeDataAdapter

        Closeme.Enabled = False
        cn = New SqlCeConnection("Data Source=\My Documents\HandheldDBWeight.sdf")
        cn.Open()
cmd = cn.CreateCommand

        Dim NumberOfRecords As Integer

NavicertsAuthor Commented:
thank you sir
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.