Method not working while pasted code does

I am on my first day with Xojo and so far it has been very intuitive. However, I have a problem I cannot figure out. For my first program I am making an address book that writes text fields and popups (eg a list of states AL, AK, CA, etc) to a mySQL community server on another PC.

I have window with the fields and such and an add button with a click event that writes the text field contents to the database. If I have the database connection code in the click event and properties set to private under that window (per the tutorials), it works fine. BUT

If I create a method with that connection code and then just call the method from the click event, the method executes, but any references to my database “db” returns nilobjectexceptions as if there is nothing “in” the db object (even if I change properties to public). To make sure the method runs, I added a message box at the end of the method that just says method executed. And that works, so the click event is running the method, but my db object is “nothing”.

I have tried all the obvious stuff I could find. I tried putting the method and properties under App, making them public, made sure to refer to it as App.db.DatabaseName etc, same nilobjectexception problem. I also tried putting the method and properties in a global module and still nilobjectexceptions.

After reading the tutorials, it seems to me this should be working.

Here is what is in my Connectdb method

Dim db As New MySQLCommunityServer
db.Host = “server ip”
db.Port = 3306
db.Databasename = “the name the database”
db.UserName = “username”
db.Password = “a really nice password”

If db.Connect Then
IsConnected = True
MsgBox(“Method Executed”)
MsgBox("Database connection error. ") + db.ErrorMessage
End If

For the properties

db As MySQLCommunityServer
IsConnected As Boolean

To make an easy test, in the click event I run the Connectdb method and then try to display the database name. Should be simple, right?


This throws the nilobjectexception, but before throwing that exception I do get the “Method Executed” message box.

If I stick the actual method code in place of Connectdb it works fine.

Left scratching my head wondering what stupid thing am I doing wrong.

you have TWO instances of DB… one that is being connected to then destroyed (when the method goes out of scope) and the other that is never connected to (or so it seems)

FYI… in the future… .enclose you code snipped using the code tags and replace square brackets with angles

[quote=416457:@Christopher Oliphant]For the properties
db As MySQLCommunityServer[/quote]
These are 2 separate and distinct variables. The first is local to the method and is destroyed later. The second is more global and what you are expecting to use in the rest of your code. Simply remove the Dim statement in your Connectdb method so it uses the global property instead.

Actually change it to

db = New MySQLCommunityServer

as you will need to create the object before you use it.