NilObjectException rs = Nil?

Hi,

When I call the method code below (which is contained inside a module) - I get returned to the debugger, and informed that a NilObjectException occurred, and that rs = Nil? (db is a property also inside the same module).

Could someone please look at the relative code segment below, in order to see why rs = Nil.

Thank you.

[code]// --------------------------------------- DEFINE THE DATABASE PATH

Dim f As FolderItem = SpecialFolder.ApplicationData.Child(“testData”).Child(“test.db”)
db = new SQLiteDatabase
db.DatabaseFile = f

// ----------------------------------------- EXECUTE THE SQL TO RETRIEVE ALL THE TITLE COLUMN VALUES

Dim ps As SQLitePreparedStatement = db.Prepare(“SELECT * FROM Test WHERE Title = ?;”)
ps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
ps.Bind(0, NewTestWindow.TitleField.text)
Dim rs As RecordSet = ps.SQLSelect

// --------------------------------------- DATABASE CONNECTION SUCCESSFUL, DISPLAY THE ERROR MSGBOX IF NAME ALREADY EXISTS

If db.connect then
If rs.RecordCount =1 then
MsgBox(“This name already exists - please rename and then retry!”)
Return

  // --------------------------------------- UNIQUE NAME, SO INSERT INTO THE DATABASE

blah blah blah[/code]

At a first quick look (leaving for the day) it looks like you are using the database before you connect to it.

I agree with Jeremy,

Try moving your:

 If db.connect then

just before the line :

Dim ps As SQLitePreparedStatement = db.Prepare("SELECT * FROM Test WHERE Title = ?;")

You might also want some error checking in there

f might be nil
f might not exist
ps may error on prepare
rs may be nil

Thanks Jeremy / Mike - that was the problem - I must have been tired :frowning:
Thanks Peter - I was going to add this after it was actually working :slight_smile:

I wish I had a dime for every time I’ve said that :smiley: