Instance error

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
I havae uaed this piece of code before in other applications that I have created and it works fine, but now I am getting the following error:

"Object reference not set to an instance of an object"

This is the code that it references as being bad:

Code:
If cnPaFAPark.State = ConnectionState.Closed Then   This is the referenced bad code line
    Call OpenParadoxFAPark()
End If

The cnPaFaPark connection is declared as a form level variable:
Code:
Private cnPaFAPark As OdbcConnection

This is the sub that I use to open the connection, the sub works fine though:
Code:
Private Sub OpenParadoxFAPark()
        Dim strCnPaFAPark As String = _
                                    "CollatingSequence=ASCII;" & _
                                    "PageTimeout=5;" & _
                                    "MaxScanRows=8;" & _
                                    "DefaultDir=P:\PDATA;" & _
                                    "FILEDSN=C:\Chester\Test_Databases\FAPark.db.dsn;DriverId=538;" & _
                                    "ParadoxNetPath=P:\PDATA;" & _
                                    "ParadoxUserName=admin;" & _
                                    "UserCommitSync=Yes;" & _
                                    "FIL=Paradox 5.X;" & _
                                    "UID=admin;" & _
                                    "Driver={Driver do Microsoft Paradox (*.db )};" & _
                                    "MaxBufferSize=2048;" & _
                                    "ParadoxNetStyle=4.x;" & _
                                    "Threads=3;" & _
                                    "SafeTransactions=0"


        Try
             // open the connection to reqtrak_data.mdb
            cnPaFAPark = New Odbc.OdbcConnection(strCnPaFAPark)
            cnPaFAPark.Open()
            MessageBox.Show("Connection to FAPark Successful!", "Connection to Paradox")
        Catch ex As Exception
             // if unable to open the connection to FAPark
            MessageBox.Show(ex.Message, "Opening FAPark Connection")
        End Try
    End Sub
I do not understand why I am now getting this error.

Any ideas
 
cpopham said:
I havae uaed this piece of code before in other applications that I have created and it works fine, but now I am getting the following error:

"Object reference not set to an instance of an object"

This is the code that it references as being bad:

Code:
If cnPaFAPark.State = ConnectionState.Closed Then   This is the referenced bad code line
    Call OpenParadoxFAPark()
End If

The cnPaFaPark connection is declared as a form level variable:
Code:
Private cnPaFAPark As OdbcConnection

This is the sub that I use to open the connection, the sub works fine though:
Code:
Private Sub OpenParadoxFAPark()
        Dim strCnPaFAPark As String = _
                                    "CollatingSequence=ASCII;" & _
                                    "PageTimeout=5;" & _
                                    "MaxScanRows=8;" & _
                                    "DefaultDir=P:\PDATA;" & _
                                    "FILEDSN=C:\Chester\Test_Databases\FAPark.db.dsn;DriverId=538;" & _
                                    "ParadoxNetPath=P:\PDATA;" & _
                                    "ParadoxUserName=admin;" & _
                                    "UserCommitSync=Yes;" & _
                                    "FIL=Paradox 5.X;" & _
                                    "UID=admin;" & _
                                    "Driver={Driver do Microsoft Paradox (*.db )};" & _
                                    "MaxBufferSize=2048;" & _
                                    "ParadoxNetStyle=4.x;" & _
                                    "Threads=3;" & _
                                    "SafeTransactions=0"


        Try
             // open the connection to reqtrak_data.mdb
            cnPaFAPark = New Odbc.OdbcConnection(strCnPaFAPark)
            cnPaFAPark.Open()
            MessageBox.Show("Connection to FAPark Successful!", "Connection to Paradox")
        Catch ex As Exception
             // if unable to open the connection to FAPark
            MessageBox.Show(ex.Message, "Opening FAPark Connection")
        End Try
    End Sub
I do not understand why I am now getting this error.

Any ideas

Are you instanciating the object before you use it, for example in the .state=closed, do you have it set to an object?

Try putting cnPaFAPark = new Odbc.OdbcConnection() to initalize it in some public domain and then see if it still errors.
 
You allocate the object in OpenParadoxFAPark, but you try to evaluate a property of the object before you create it (If cnPaFAPark.State = ConnectionState.Closed). Your design is flawed. The easiest solution would be to allocate the object at construction:


Private cnPaFAPark As New OdbcConnection

and set the connection string in that obscurely named function

Try
// open the connection to reqtrak_data.mdb
cnPaFAPark.ConnectionString = strCnPaFAPark
cnPaFAPark.Open()

But then again that is bad design.
Why dont you allocate the object in the constructor, set it to null and then check for null.
 
Thanks, yeah, I got out of sequence. I usually instanciate and open the connection one time before I start checking to see if the connection is still open. I guess I had been looking to long and just missed it :) Chester
 
Back
Top