Error checking for NULL in VBScript

137,368

Solution 1

From your code, it looks like provider is a variant or some other variable, and not an object.

Is Nothing is for objects only, yet later you say it's a value that should either be NULL or NOT NULL, which would be handled by IsNull.

Try using:

If Not IsNull(provider) Then 
    url = url & "&provider=" & provider 
End if

Alternately, if that doesn't work, try:

If provider <> "" Then 
    url = url & "&provider=" & provider 
End if

Solution 2

I see lots of confusion in the comments. Null, IsNull() and vbNull are mainly used for database handling and normally not used in VBScript. If it is not explicitly stated in the documentation of the calling object/data, do not use it.

To test if a variable is uninitialized, use IsEmpty(). To test if a variable is uninitialized or contains "", test on "" or Empty. To test if a variable is an object, use IsObject and to see if this object has no reference test on Is Nothing.

In your case, you first want to test if the variable is an object, and then see if that variable is Nothing, because if it isn't an object, you get the "Object Required" error when you test on Nothing.

snippet to mix and match in your code:

If IsObject(provider) Then
    If Not provider Is Nothing Then
        ' Code to handle a NOT empty object / valid reference
    Else
        ' Code to handle an empty object / null reference
    End If
Else
    If IsEmpty(provider) Then
        ' Code to handle a not initialized variable or a variable explicitly set to empty
    ElseIf provider = "" Then
        ' Code to handle an empty variable (but initialized and set to "")
    Else
        ' Code to handle handle a filled variable
    End If
End If
Share:
137,368
Vivian River
Author by

Vivian River

Updated on August 13, 2020

Comments

  • Vivian River
    Vivian River almost 4 years

    I have the following VBScript in a Classic ASP page:

    function getMagicLink(fromWhere, provider)
        dim url 
        url = "magic.asp?fromwhere=" & fromWhere
        If Not provider is Nothing Then ' Error occurs here
            url = url & "&provider=" & provider 
        End if
        getMagicLink = "<a target='_blank' href='" & url & "'>" & number & "</a>"
    end function
    

    I keep getting an "Object Required" error messager on the line that says If Not provider Is Nothing Then.

    Either the value is NULL, or it's not NULL, so why am I getting this error?

    Edit: When I invoke the object, I pass in either NULL, or I pass in a string.

  • Vivian River
    Vivian River over 11 years
    I tried using If Not IsNull(provider) Then, but then the page raises an exception on url = url & "&provider=" & provider line. The error says, "Object variable not set".
  • LittleBobbyTables - Au Revoir
    LittleBobbyTables - Au Revoir over 11 years
    That's a head-scratcher. Are you passing in vbNull to provider, or something else?
  • LittleBobbyTables - Au Revoir
    LittleBobbyTables - Au Revoir over 11 years
    I was always taught Nothing is for objects only, try passing in vbNull instead. Nothing <> Null in VBScript.
  • Cheran Shunmugavel
    Cheran Shunmugavel over 11 years
    Use Null, not vbNull. vbNull is a constant that is used with the VarType function.