[asp-classic] Error checking for NULL in VBScript

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.

The answer is


I will just add a blank ("") to the end of the variable and do the comparison. Something like below should work even when that variable is null. You can also trim the variable just in case of spaces.

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

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

Examples related to asp-classic

File URL "Not allowed to load local resource" in the Internet Browser How to prevent 'query timeout expired'? (SQLNCLI11 error '80040e31') Getting Error 800a0e7a "Provider cannot be found. It may not be properly installed." Regex to split a CSV Update div with jQuery ajax response html Error checking for NULL in VBScript How to enable ASP classic in IIS7.5 How to resolve "The requested URL was rejected. Please consult with your administrator." error? Response Buffer Limit Exceeded Detailed 500 error message, ASP + IIS 7.5

Examples related to vbscript

How to run VBScript from command line without Cscript/Wscript How to set recurring schedule for xlsm file using Windows Task Scheduler How to prevent 'query timeout expired'? (SQLNCLI11 error '80040e31') How do I rename a file using VBScript? How to get two or more commands together into a batch file How to run vbs as administrator from vbs? Find specific string in a text file with VBS script Getting current directory in VBScript Run Command Line & Command From VBS Permission denied on CopyFile in VBS

Examples related to null

getElementById in React Filter values only if not null using lambda in Java8 Why use Optional.of over Optional.ofNullable? How to resolve TypeError: Cannot convert undefined or null to object Check if returned value is not null and if so assign it, in one line, with one method call How do I assign a null value to a variable in PowerShell? Using COALESCE to handle NULL values in PostgreSQL How to check a Long for null in java Check if AJAX response data is empty/blank/null/undefined/0 Best way to check for "empty or null value"

Examples related to nullreferenceexception

How to solve Object reference not set to an instance of an object.? Value cannot be null. Parameter name: source Error checking for NULL in VBScript Checking session if empty or not Checking if an object is null in C# What is a NullReferenceException, and how do I fix it? C# elegant way to check if a property's property is null What does "Object reference not set to an instance of an object" mean?

Examples related to nothing

Error checking for NULL in VBScript VBA: Conditional - Is Nothing Why can't I check if a 'DateTime' is 'Nothing'? IsNothing versus Is Nothing