Getting InvokeMethodOnNull when object exists

Welcome Forums General PowerShell Q&A Getting InvokeMethodOnNull when object exists

This topic contains 8 replies, has 2 voices, and was last updated by

2 years, 4 months ago.

  • Author
  • #52901

    Points: 0
    Rank: Member

    Hi Scripters! I'm working with Internet Explorer automation to launch and log on to two websites. I can successfully create the IE COM objects, pass them to a "login" function, and see that the site accepts the credentials. However, afterward I would like to periodically refresh the page in order to maintain the session. When I pass the existing IE objects to my "test-loginPage" function (designed to check if we're back at the login page, and either login again else refresh the current page), I consistently get the InvokeMethodOnNull error when trying to use the object's Refresh method. If I use the ISE debugger and put a breakpoint at the beginning of the test-loginPage function, I can verify that the object has been passed to the function, that it's still an IE object, and that Get-Member indicates it has a Refresh method. What am I doing wrong?

    Here's the Gist; I'm running Windows 10.1511, so Powershell 5 interacting with IE 11.
    Note: I did change the stored logon information so that it is not valid credentials, and therefore commented out the part of the test-loginPage function that would normally be checking if we're still on the login page. I still expect the code to fill in the login fields but I expect the site in question will fail the logon attempt.

    Thanks in advance!

  • #52936

    Points: 1
    Rank: Member

    What's the full error message you receive?

  • #52995

    Points: 0
    Rank: Member

    You cannot call a method on a null-valued expression.
    At C:\Users\\Desktop\Watchdesk2.ps1:103 char:13
    + $ieObjToTest.Refresh()
    + ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    Sorry, I forgot to include that the first time.

  • #53109

    Points: 1
    Rank: Member

    I can't replicate the issue you're facing. The Refresh method works for me without error.

    If you:
    – comment out this line: login-website $startSite $ie
    – and also keep the switch conditions in test-logonPage commented out as you had above,

    does the fault still occur?

  • #53166

    Points: 0
    Rank: Member

    Yes, same error. Also the same if I directly call the refresh method of the $ieTS/$ieTex objects instead of passing them to the test-logonPage function.

    If I write out just the "launch website and refresh it" part, it works, as in:

    $telestaff = ""
    $ie = New-Object -com InternetExplorer.Application
    $ie.Visible = $true
    $texcom = ""
    $ie2 = New-Object -com InternetExplorer.Application
    $ie2.Visible = $true
    Start-Sleep -s 60

    Launches 2 IE windows, loads the appropriate pages, waits, and successfully refreshes both.

    BUT, running only the "start-IE" function (logon still commented out) and then calling the refresh method on the returned object fails, even if I launch only a single page. So, if I pull just this out of the script and run it, I get the error:

    $texcom = ""
    Function Start-IE($startSite) 
        $ie = New-Object -com InternetExplorer.Application
        $ie.Visible = $true
        #wait for page to load
        while ($ie.Busy) 
            start-sleep -s 1
        #login-website $startSite $ie
        Return $ie
    $ieTex = Start-IE $texcom
    Start-Sleep -s 60

    ...Something about transporting the IE object out of the function? I still get what looks like the object if I ask for $ieTex after running the above code:
    PS C:\Users\jflanagan> $ieTex

    Application : System.__ComObject
    Parent : System.__ComObject
    Container :
    Document : mshtml.HTMLDocumentClass
    TopLevelContainer : True
    Type : HTML Document
    Left : 294
    Top : 0
    Width : 2511
    Height : 1445
    LocationName : Login | TexCom
    LocationURL :
    Busy : False
    Name : Internet Explorer
    HWND : 199788
    FullName : C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
    Path : C:\Program Files (x86)\Internet Explorer\
    Visible : True
    StatusBar : True
    StatusText :
    ToolBar : 1
    MenuBar : True
    FullScreen : False
    ReadyState : 4
    Offline : False
    Silent : False
    RegisterAsBrowser : False
    RegisterAsDropTarget : True
    TheaterMode : False
    AddressBar : True
    Resizable : True

  • #53368

    Points: 1
    Rank: Member

    I've build a Windows 10 test VM for this, JFlanagan, and now get the same issue.

    I was using IE11 on Windows 8.1 and didn't get the error.

    Will have a play around with it today.

  • #53371

    Points: 1
    Rank: Member


    So simply do this:

    Change: $ieTex.Refresh()
    to: $ieTex.Application.Refresh()

    Tested this change works on a Windows 10 VM.

  • #53580

    Points: 0
    Rank: Member

    Okay, that was pretty simple and it works for refreshing the page. Thanks 84rusty!

    Can you point me at an explanation of when/why I would need to use the .Application part? I'd like to learn and so far I'm failing at searching for it.

    • #53598

      Points: 1
      Rank: Member

      I can't find an explanation either 🙁

      Maybe someone else can weigh in on this.

The topic ‘Getting InvokeMethodOnNull when object exists’ is closed to new replies.