Database problem

I am getting a NilObject Exception with rs, can anyone help, thanks

[quote] Dim dr as new DatabaseRecord
dim rs as recordset = golfer.SQLSelect("select * from course where PK = "+ str(coursepk))[/quote]

The database is there I created it earler.

If you want to be able to step through code and catch these problems yourself, try writing more fail-safe, debuggable code:

[code]dim dr as DatabaseRecord
dim rs as RecordSet

if golfer <> nil then
rs = golfer.SQLSelect("select * from course where PK = "+ str(coursepk))
end if[/code]

Set a breakpoint on the first line, and step through it.

And store your sql in a local variable so you can examine the assembled string. And check for database errors.

But if the error is on the second line you posted, then golfer is nil. A common reason for this kind of error is that you dimmed a local variable name “golfer” when you created the database, which leaves the global variable nil.

Hi Tim, this is my create Database code :

[code] golfer = New SQliteDatabase

dim f as folderitem = specialFolder.ApplicationData.Child(“TGC”)
if f.exists = false then
f.createasfolder
end

f = f.child(“Golf”)
if f.exists = false then
f.createasfolder
end

f = f.child(“golfer.rsd”)
golfer.DatabaseFile = f

// Set Database File
golfer.DatabaseFile = SpecialFolder.ApplicationData.Child(“TGC”).Child(“Golf”).Child(“golfer.rsd”)

// Connect to the database
if golfer.databaseFile.exists = true then
// The database file already exists, so we want to connect to it.
if golfer.Connect() = false then
DisplayDatabaseError//( false )// there was an error connecting to the database
Quit
return
end if

else
// The database file does not exist so we want to create a new one.
// The process of creating a database will establish a connection to it
// so there isn’t a need to call Database.Connect after we create it.

CreateDatabaseFile

end

// Set the application to AutoQuit, so if all windows are closed then the application
// will quit.
app.autoQuit = true[/code]

and

[code] if golfer.CreateDatabaseFile = false then

// Error While Creating the Database
MsgBox "Database Error" + EndOfLine + EndOfLine + "There was an error when creating the database."
Quit

end if

golfer.SQLExecute “CREATE TABLE course(PK INTEGER PRIMARY KEY NOT NULL ,club, clubaddress,proshop,clubphone,par1,par2,par3,par4,par5,par6,par7,par8,par9,par10,par11,par12,par13,par14,par15,par16,par17,par18,parfront,parback,partotal)”
golfer.SQLExecute “CREATE TABLE mens(PK INTEGER PRIMARY KEY NOT NULL ,mens1,mens2,mens3,mens4,mens5,mens6,mens7,mens8,mens9,mens10,mens11,mens12,mens13,mens14,mens15,mens16,mens17,mens18,mensfront,mensback,menstotal)”
golfer.SQLExecute “CREATE TABLE ladies(PK INTEGER PRIMARY KEY NOT NULL ,ladies1,ladies2,ladies3,ladies4,ladies5,ladies6,ladies7,ladies8,ladies9,ladies10,ladies11,ladies12,ladies13,ladies14,ladies15,ladies16,ladies17,ladies18,ladiesfront,ladiesback,ladiestotal)”
golfer.SQLExecute “CREATE TABLE blue(PK INTEGER PRIMARY KEY NOT NULL,blue1,blue2,blue3,blue4,blue5,blue6,blue7,blue8,blue9,blue10,blue11,blue12,blue13,blue14,blue15,blue16,blue17,blue18,bluefront,blueback,bluetotal)”
golfer.SQLExecute “CREATE TABLE red(PK INTEGER PRIMARY KEY NOT NULL,red1,red2,red3,red4,red5,red6,red7,red8,red9,red10,red11,red12,red13,red14,red15,red16,red17,red18,redfront,redback,redtotal)”
golfer.SQLExecute “CREATE TABLE white(PK INTEGER PRIMARY KEY NOT NULL,white1,white2,white3,white4,white5,white6,white7,white8,white9,white10,white11,white12,white13,white14,white15,white16,white17,white18,whitefront,whiteback,whitetotal)”
golfer.SQLExecute “CREATE TABLE yellow(PK INTEGER PRIMARY KEY NOT NULL,yellow1,yellow2,yellow3,yellow4,yellow5,yellow6,yellow7,yellow8,yellow9,yellow10,yellow11,yellow12,yellow13,yellow14,yellow15,yellow16,yellow17,yellow18,yellowfront,yellowback,yellowtotal)”
golfer.SQLExecute “CREATE TABLE shop(PK INTEGER PRIMARY KEY NOT NULL,busname,busaddress,busphone,buswebsite,busemail,bushoursmf,bushoursw,fittingyes,fittingno,coachingyes,coachingno)”
golfer.Commit[/code]

Thanks

Yeah, the built-in debugger is going to make more sense out of this than anyone here. Set breakpoints and trace through it.

Always check the database.error bit after every db operation. It will tell you tons.

How do I make sure the Database is open, thanks.

I am now coming up with NilObject exception at "dr.column(“club”) = trim(club.text)

[code] dim dr as DatabaseRecord
dim rs as RecordSet

if golfer <> nil then
rs = golfer.SQLSelect("select * from course where PK = "+ str(coursepk))
end if

dr.column(“club”) = trim(club.text)
dr.column(“clubaddress”) = trim(clubaddress.text)
dr.column(“proshop”) = trim(proshop.text)[/code]

You have not created a “New DatabaseRecord” in that code, so dr will be nil and dr.column will throw an exception.

Have you done as Brad suggested and stepped through the code in the debugger?

Yes and I get Nil at dim dr as DatabaseRecord and nilobjectexception atdr.column

I am using the code from another program I wrote that worked fine, have changed the names to suit the new program and am getting the errors. I copied the code line for line but still can’t find where the problem is.

You need either

dim dr as New DatabaseRecord

or

dr = New DatabaseRecord

Also: is golfer non-nil? Do you get a valid rs?

Hi Tim thanks that got rid of the error at the top of the code, now I have an Nilobjectexcetion error at the boton in the line

[quote] dim dr as New DatabaseRecord
dim rs as RecordSet

if golfer <> nil then
rs = golfer.SQLSelect("select * from course where PK = "+ str(coursepk))
end if

dr.column(“club”) = trim(club.text)
dr.column(“clubaddress”) = trim(clubaddress.text)
dr.column(“proshop”) = trim(proshop.text)
dr.column(“clubphone”) = trim(clubphone.text)
dr.column(“par1”) = trim(par1.text)
dr.column(“par2”) = trim(par2.text)
dr.column(“par3”) = trim(par3.text)
dr.column(“par4”) = trim(par4.text)
dr.column(“par5”) = trim(par5.text)
dr.column(“par6”) = trim(par6.text)
dr.column(“par7”) = trim(par7.text)
dr.column(“par8”) = trim(par8.text)
dr.column(“par9”) = trim(par9.text)
dr.column(“par10”) = trim(par10.text)
dr.column(“par11”) = trim(par11.text)
dr.column(“par12”) = trim(par12.text)
dr.column(“par13”) = trim(par13.text)
dr.column(“par14”) = trim(par14.text)
dr.column(“par15”) = trim(par15.text)
dr.column(“par16”) = trim(par16.text)
dr.column(“par17”) = trim(par17.text)
dr.column(“par18”) = trim(par18.text)
dr.column(“parfront”) = trim(parfront.text)
dr.column(“parback”) = trim(parback.text)
dr.column(“partotal”) = trim(partotal.text)
golfer.InsertRecord “course”, dr ************** Nilobject exception HERE

golfer.commit
msgbox"New Record Added"[/quote]

i

Shane, pretty please: use the debugger to find out what is NIL. Rather likely, the “golfer” is the culprit. Try the following structure for your code:

[code]if golfer = nil
'some nice error message here
return
end if

'now do your update here[/code]

Bonus:

  • Your databases looks like it needs some optimizations. par1 to par18 need to go to an extra table.
  • Check control array in the documentation.

HTH

Hi Beatrix, I have tried the debugger and am lost with that as well, I am only learning and slowly at that. Thanks for the code, but I really have no idea where to put it. Thanks Shane.

Learning is hard :slight_smile:

The debugger is your most important tool when developing. If you have an exception then the debugger stops at the line where the exception occurs. In your case that is most likely the “golfer” variable, where you are trying to talk to something that isn’t there.

My code was pseudo-code for the code that you showed in your last example. It wasn’t meant to be taken literally and should just show the structure: check your most important variable, here the database. Give the user an error message. Don’t do anything else in the method and just return.

Please have a good and thorough look at the debugger part of the user guide.

Read all the users guides (including the free book learning to program)
Go through the examples in the user guide
Do the tutorials
Learn the debugger

Not doing that will lead you to being very frustrated as it will be hard to make much progress on your own without knowing how the debugger can help you

I work here and I can’t imagine trying to do this job without being in the debugger for hours and hours on end every day
It IS what helps me retain what little sanity I have left :stuck_out_tongue:

Ok what I came up with with the Debugger is

dr Nil
rs Nil

and at the end of the code in this line

golfer.InsertRecord “course”, dr

dr database record nilobjectexception

All I can assume is the database is not open.

The code is Identical to the code in the other program I wrote, but the other program executes fin.

Thanks Shane