New to Xojo :- An exception of class NilObjectException was not handled

I’m following a youtube tutorial (https://www.youtube.com/watch?v=h3_zKkAz1Tk) on how to use mysql with Xojo and am getting the above issue.

This happens on the following line

var columns as RowSet = db.TableColumns(selected_tbl)

I’ve tried changing the selected_tbl variable to a string (EG a table name in the database).

The tutorial is from two years ago, maybe TableColumns is not used this way any longer

I am using a mac and the latest version of Xojo, i can connect to the mysql server no problem and can populate a popupMenu with this code.

for each row as DatabaseRow in dbtable
** popupMenu1.AddRow(row.ColumnAt(0).StringValue)**
next

With xojo making languaje changes it is really hard to find tutorial outside the xojo ones.

In this case, the tutorial is not a good one, a NilObjectException is when you have a reference but the object was not created or was destroyed.

In this case, looks like the db object is the problem.

You should verify if the object is initialized befor using it.

If db = nil Then
'Database not connected
Else
'Do stuff with db
End if

NilObjectException is something like
var columns as RowSet = Nil.TableColumns("TableName")

Thank you everyone for the replies.
Ivan, it was indeed the db object, thank you.

This was declared in the “opening” method of Window1
Var db As New MySQLCommunityServer

and there is a db “Property” (public) in the “opening” event handler of Window1.

This is exactly how the code is laid out in the youtube tutorial.

I come from a Lazarus/ Pascal background and would normally use a global variable if I wanted to access the variable in a different window / control (although to access a mysql server i would use zconnection, zquery and datasource components so to make a connection it is totally different)

I dont mind admitting I’m struggling a little to get my head around the differences in the IDE / the way things work.

Is there a setting in the IDE to make declared variables or poroperties global, or should i be using the property / declaration differently ?.

Is there a good working example of a mysql app ?

Thanks for all your help everyone.

you can use a global modul property or a property in the app (for desktop projects)
you can also sub class this database class to make it more handier.

I managed to sort this myself after reading various posts on the forum.

For anyone else struggling, here is how i did it (happy to hear any other solutiuons).

From the Xojo menu choose insert / module, this will give you an icon called Module1 (in the contents window if that is what it’s called), there’s a hint here that this is global as the icon is one of planet earth,

In this module, create a property called db and give it a type of MySQLCommunityServer.

Change the declaration of the variable from “var db As New MySQLCommunityServer” (this would be a declaration of a local variable) to “db = NEW MySQLCommunityServer”

Works for me, hope it helps someone at sometime.

2 Likes

Not quite:
image


The db property is not in the opening event handler, it is a window1 property.

1 Like

Yes, sorry, what i should have said is
“there is a db “Property” (public) initialized in the “opening” event handler of Window1”

Thanks for pointing out this error.

Why are they called properties ?, why not just call them variables, would make the transition a little easier.

1 Like

Because they behave differently. Variables go out of scope at the end of a method. Properties live on the object you’ve defined them on. A Module property lives forever, a Class property lives for the duration of the class instance.

Have you seen the Introduction to Xojo Programming Textbook?

2 Likes