Help with Windows version of app "nil object exception"

Hi

I have an application that I have developed on the Mac, and it is running fine. It includes opening two databases when the App launches, this happens OK, and I can get the data from the databases. The databases are stored in the SpecialFolder.ApplicationData folder.

I have transferred the program to my Virtual Windows box on my Mac. As I step through the code in the debugger I can see that both databases are found and opened. However, on the Windows machine, I get a NilObjectException from this code:

[code] Dim i As Integer
Dim sql As String
Dim data As RecordSet

sql = “SELECT * FROM category”
data = catClassesdb.SQLSelect(sql)
i = 1
If data <> Nil Then
While Not data.EOF
BirdsContCon1.selectCatPopup.AddRow(data.Field(“cat_name”).StringValue)
i = data.Field(“cat_id”).IntegerValue
data.MoveNext
Wend
End If[/code]

I t works perfectly on the Mac. Can anyone see something wrong with my code, or have any suggestion as to what might be wrong?

Can’t see anything obvious, which line does the exception occur on?

Could be i = data.Field(“cat_id”).IntegerValue which will raise the similar “Invalid use of a null” error Try doing,
i = “” + 1data.Field(“cat_id”).IntegerValue

Hi Wayne

It occurs on this line:

data = catClassesdb.SQLSelect(sql)

Except that, obviously it is a different program, I am sure that I have done nothing different to two other programs I have written. And, of course, it works on the Mac.

Chris, The problem occurs before that line. I will keep a note of it in case it does cause an error when I resolve the other problem.

I was thinking of re-connecting to the databases, but because of my poor knowledge of Windows I cannot find how to get to the AppData folder from the Open dialog box. It seems that %AppData% can’t be used.

I gave you a bit of a bum steer suggesting i = “” + 1data.Field(“cat_id”).IntegerValue try i = 0 + 1data.Field(“cat_id”).IntegerValue. Have you tested that you have a database connection by testing with the db.connect function?

Hi Chris,

Well, I didn’t have that code in, but I put it in and for both databases I got the MsgBox with “Connected to [and the database name]”

Use specialfolder.ApplicationData to map to the folder. It’s obvious that the issue is where the catClassesdb is opened. A classic mistake is to have a property catClassesdb and also have Dim catClassesdb As New sqlitedatabase which will make the connect skoped to that method.

[quote=103138:@Cliff Strange]It occurs on this line:
data = catClassesdb.SQLSelect(sql)[/quote]
This means that catClassesdb is Nil. Either you are not creating it prior to calling this code, or maybe you have two instances by mistake (for example a local one which “hides” the global one - and the global one is the valid one).

OK, I understand that, and I have checked, and I don’t think I have two instances - I am following what is in the different database tutorials online, and what has worked for me before. In a Module I have a SQLite database property (two in fact for this app - both with different names). Then in the App scope I have connected to each of the existing databases which are stored in the Application Data folder.

Here is the code for connecting to one of the database files:

[code] 'Firstly get class names into array
Dim dbFile As FolderItem
dbFile = SpecialFolder.ApplicationData.Child(“Poultry”).Child(“catClasses_db.sdb”)
If dbFile <> Nil Then
Dim db As New RealSQLDatabase
db.DatabaseFile = dbFile
If db.Connect Then
MsgBox("Connected to " + dbFile.Name)
Else
MsgBox("Error: " + db.ErrorMessage)
End If
End If

Dim classesDB As New SQLiteDatabase
classesDB.DatabaseFile = dbFile
If Not classesDB.Connect Then
MsgBox("Could not connect to " + dbFile.Name)
Exit
End If
[/code]

The other one is essentially the same, just changed the name of the file.

The thing that confuses me is that this app works perfectly on the Mac, and furthermore, I am using exactly the same methods and code (except for details) as I have done in the past, and everything has worked as I expected it to.

Why are you creating two database instances for the same file, a RealSQLDatabase and an SQLiteDatabase instance?

I don’t understand why you are using two connections either.

However, you are Dim the classesDB here. classesDB should be a global variable so that it can be accessed outside the App.Open event. The way you have it it is destroyed as soon as App.Open exits. Then you will get a NilObjectException on a subsequent usage.

Now I’m getting confused. I have the classesDB as a global variable of type SQLiteDatabase. In App, I have catClassesdb which I have used to create the tables and columns in the database.

So what should I do now? Do I delete the catClassesdb in App and just use classesDB in the module? If I do that, how do I connect with the database?

Why does it work on the Mac and not on the PC? And finally, with the exception of the names of the different database connections I have got, I am using the same method as in other apps I have running.

I did get rid of the second Dim classesDB as New SQLiteDatabase in the code above, and it made no difference.

You showed this code above:

Dim classesDB As New SQLiteDatabase <-- Reclassified here classesDB.DatabaseFile = dbFile If Not classesDB.Connect Then MsgBox("Could not connect to " + dbFile.Name) Exit End If
If classesDB is a global then you have redeclared it here with the Dim statement. Your code should look like this (if classesDB is a global variable):

classesDB = New SQLiteDatabase <--- Changed Here!! classesDB.DatabaseFile = dbFile If Not classesDB.Connect Then MsgBox("Could not connect to " + dbFile.Name) Exit End If

Might it be that you develop on OS X and that the db file was pre-existing, so you never tested the creation by code? Because the code you have showed here, doesn’t create the file.

Thank you everyone for your help. I was getting confused between the file properties in Xojo that I had used to create the database tables etc, and the Global Properties that are the two actual databases. It turns out on the Mac I can use them to connect to, and use the database, but Windows is a little more fussy.

I had another look at one of Paul Lefebvre’s Webinars and immediately saw where my mistakes occurred. My app now works on Windows as well as the Mac. The UI is fairly simple so I didn’t have to make any changes to it.

I love Xojo again!