Object variable not set

You tried to access an instance of a LotusScript® class or product class, but either of the following was true:

  • The object reference variable you used does not hold a reference to any object. (Its value is NOTHING.) This can happen either because the variable was declared but never assigned, or because it was assigned the value Nothing (this value is returned by some built-in methods to indicate failure).

    Use the Set statement to assign the variable a reference to an object.

  • The object reference variable has been deleted. This can happen either explicitly (through the Delete statement) or as a side effect of another variable being Deleted or going out of scope. We discuss this in more detail as follows.

    Remove the statement that refers to the deleted variable.

The variable causing the problem, is generally the one that precedes the "." -- for instance, if this error occurs on the following line:

Set doc = view.GetDocumentByKey(keys(0))

then the problem is that the variable view has the value Nothing. The coding error that caused the problem is probably not on this line; it's that you failed to assign or check the value of the variable before you got to this line. Possibly the variable was never assigned, or perhaps you have misspelled the name here, or perhaps it has been assigned with the value Nothing. For instance, if the variable was earlier assigned with the statement:

Set view = db.GetView("lkByDocID")

and there is no view named lkByDocID, there is not an error at that time, but the variable view is assigned the value Nothing. This sets you up to have an error later when you use the expression view.anything.

To avoid having this problem:

Use Option Declare in all your scripts. This will catch any misspellings of variable names at compile time, saving much time.

Use Is Nothing to test whether the object variable has a useful value before you try to use it.

Or, use On Error to trap the error and take corrective action (or at least print a more detailed error message).

Pay attention to where object variables are deleted and think about what other objects might depend on them.

One surprising situation where this error can occur, is where the object is returned by a subroutine, but the object is dependent on another variable that goes out of scope when the subroutine exits. For instance, consider the following:

Function GetKeywordView( ) As NotesView
	Dim db As New NotesDatabase("Annie", "bigdeal\keywords.nsf")
	Set GetLookupView = db.GetView("AllKeywords")
	If GetLookupView Is Nothing Then
		Error ERR_KW_VIEWMISSING, "View AllKeywords was not found!"
	End If
End Function

On the face of it, this routine looks pretty safe -- we're testing whether the view was found and throwing an error if it is not. But when we call this function and try to use its value, we find that the function returned Nothing:

Set keyView = GetKeywordView()
Set keyDoc = keyView.GetDocumentByKey("Departments") ' Object variable not set error here.

How can keyView have the value of Nothing at this point? The answer is that a NotesView object cannot exist without its parent NotesDatabase object. On exiting the GetKeywordView function, the variable db is automatically deleted because the stack is freed. And when you delete db, all objects that you created from db (NotesView, NotesDocument, NotesACLEntry, etcetera) are also deleted.

To prevent this happening, you could either declare db in the caller, or make it global or static.