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("Database connection error. ") + db.ErrorMessage
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.