Why Do I Keep Getting a NilObjectException?

Perhaps this topic will be helpful:


adding a property does not by default initalize (except in the case of simple number (0) or strings (""))
other object such as a database need to be instantiated ( db = new SQLitedatabase for example), but even that while creating an instance of the database does not connect it to a database structure ( DB.CONNECT)…

If the property is a simple data type like an integer, you can just go ahead and use it.
Classes such as SQLiteDatabase, folderitem, dictionary… they all need to be initialised.
Usually in App.open event
If not there, then ‘just in time’ like this

if App.DB = nil then msgbox "The DB object had not been initialised by this point" App.DB = new SQLiteDatabase end if

At the error point Session.DB is nil. This has nothing to do with the folderitem.

Before the If End If block insert the line

Session.DB = New SQLItedatabase

In your If Else section you are missing a connect statement

If Session.DB.Connect = False Then Msgbox ("Database file Exists, but can't connect to it") End If

Remove the lines

Dim db As New SQLIteDatabase

It is not used and could be confusing and

Session.DB.DatabaseFile = NewDatabaseFile

near the end.

You should also insert error checking after each SQLExecute and after Commit.

Your placement of the database file in the resources folder is going to cause you problems when run in the Program Files folder as resources will be read only. I would use applicationdata instead.


Remember, you can’t use the global Session function in a Session.Open event. (source)
There’s your NOE :slight_smile:

(Thanks to Wayne who pointed out the obvious, that Session was nil. Wouldn’t have seen it otherwise.)