Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if object Is Nothing results in 'Object required' error

I am supporting some classic ASP pages, one of which uses and re-uses an object conn and disposes of it either when the .asp page finishes processing or right before the page redirects to another page.

<%
dim conn
...
set conn = server.CreateObject("adodb.connection")
...
sub cleanUp()
    conn.Close
    set conn = nothing
end sub
...
sub pageRedirect(url)
    call cleanUp()
    response.Redirect url : response.End
end sub
...
' very end of file
call cleanUp()%>

I've found that if there is a redirect, I get a server error right at the line conn.Close, Microsoft VBScript runtime error '800a01a8' Object required. I figure there's no reason why that line would execute more than once but to be safe I rewrote the function

sub cleanUp()
    if(not (conn Is Nothing)) then
        conn.Close
        set conn = Nothing
    end if
end sub

But I still get that exact error, now at the line if(not (conn Is Nothing))!! I thought the purpose of Is Nothing was to do a test before using the variable name conn precisely to prevent that 'object required' error, but the test is throwing the same error.

What other test can I use to make sure conn isn't referenced if it'd already been set to Nothing?

like image 523
East of Nowhere Avatar asked Mar 12 '12 15:03

East of Nowhere


2 Answers

is nothing is used to test for an object reference, if the variable does not contain such then the test is invalid & raises an error, so conn can only be tested after its been set to something.

You can;

if isobject(conn) then
   if not (conn Is Nothing) then set conn = nothing
end if
like image 53
Alex K. Avatar answered Oct 11 '22 23:10

Alex K.


  1. Use option explicit (each time a script runs without option explicit, a puppie dies out there), you probably would have detected the problem earlier as Nilpo mentioned.
  2. When you dim a variable that you are going to use as an object reference and test it on Nothing, make it a habbit to set it to Nothing at initialization time(*): dim myObject : Set myObject = Nothing.

(*) Not really at initialization, because the dim's are handled before a routine starts, but when you put all your dim's at the top of a routine, it will practically be the same.

like image 43
AutomatedChaos Avatar answered Oct 11 '22 22:10

AutomatedChaos