0

When I run the program this function is in I get a cryptic error that doesn't make sense to me. I wasn't aware I was running a method on a null-valued expression. It occures to me that this is either a scope issue or a value is not getting set. I however have not been able to figure it out and put it out to the community:

You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\DCB Settings Modification\DCBxPowershell.ps1:747 char:21
+                 If ($resetAdapter -eq $FAIL_RESULT){
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\DCB Settings Modification\DCBxPowershell.ps1:760 char:17
+             If ($resetAdapter -eq $FAIL_RESULT){
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull**

$FAIL_RESULT = 0
$PASS_RESULT = 1

Function Use-Menu
{ 
    param($DCBmenuItems, $modificationCatagoryChoosen)

    ## Function Menu-Choose --## Holds choosen Network Interface Index to work with            
    $networkIndex = Menu-Choose $strippedNetworkIndex $networkChooseTitle
    #$networkIndex[0]  ### Debug -
    $resetAdapter = $FAIL_RESULT

    Start-Sleep -s .7

    If ($result = $PASS_RESULT) {
    ## Find Current Config in order to display it to user
        $dcbConfig = Find-Config $networkIndex
    }

    #The following 'DO WHILEs' are for the "Go back to previous Menu" functionality.
    Do {
        Do {

            ## Function Menu-Choose --## Let user choose which catagory of modification to perform
            $modificationCatagoryChoosen = Menu-Choose $DCBmenuItems $DCBMenuTitle $networkIndex -scope global
            If (($DCBmenuItems.count - 1) -eq $modificationCatagoryChoosen) {
                $resetAdapter = $PASS_RESULT
            }

            # These If Then statements allow reset of adapter without changing settings
            If ($resetAdapter -eq $FAIL_RESULT){
                $DCBmenuItems2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Option
                $DCBMenuTitle2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Name

                Start-Sleep -s .7

                ## Function Menu-Choose --## Let user choose which modification to perform
                $modificationChoosen = Menu-Choose $DCBmenuItems2 $DCBMenuTitle2 $networkIndex
            }

        } While (($modificationChoosen -eq $DCBmenuItems2.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT))

        # These If Then statements allow reset of adapter without changing settings
        If ($resetAdapter -eq $FAIL_RESULT){ 
            ## Changes the options to choose on Menu-Choose to last chosen catagory
            $DCBmenuItems3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Option
            $DCBMenuTitle3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Name

            Start-Sleep -s .7

            ## Function Menu-Choose --## Let user choose how to modify DCB setting
            $modificationOptionChoosen = Menu-Choose $DCBmenuItems3 $DCBMenuTitle3 $networkIndex
        }
    } While (($modificationOptionChoosen -eq $DCBmenuItems3.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT)) 

    # These If Then statements allow reset of adapter without changing settings
    If ($resetAdapter -eq $FAIL_RESULT){
        Start-Sleep -s .7

         ## Function Set-RegistryValues --## Records modified DCB setting to registry
        Set-RegistryValues $xmlDCBregEdits $modificationCatagoryChoosen $modificationChoosen $modificationOptionChoosen
    }

Return $networkIndex
IkeaTheSofa
  • 63
  • 1
  • 1
  • 6
  • I realize this is an old post, BUT, I notice here, and below, you have the line `If ($result = $PASS_RESULT) {`. The variable `$result` is not used anywhere else - it is not at all clear what this statement if for. But most importantly, did you mean `If ($result -eq $PASS_RESULT) {`? And was this perhaps a "translation" typo when you stripped down your code for presentation here, and if so, might this have had something to do with your issue? Just asking, because `If ($result = $PASS_RESULT) {`in this context is just bad. – David I. McIntosh Nov 30 '16 at 02:31

3 Answers3

1

It appears that changing from two if-then statements at first to inserting breaks after my initial if-then statement and adding an identical one on the second Do-Loop fixed the problem I was having. The reason I did this was that after going down one layer of my menu and then returning to the top layer I notice the error was absent when choosing reset-adapter.

Side Bar: I didn't explain that the function I listed here was a menu that iterates through the $DCBmenuItems, although this may have been obvious from the code.

I think $resetAdapter wasn't getting set correctly for some reason. I'm not 100% sure as to why this solved my problem and why the other code threw errors. Here is the amended code:

$FAIL_RESULT = 0
$PASS_RESULT = 1

Function Use-Menu
{ 
    param($DCBmenuItems, $modificationCatagoryChoosen)

    ## Function Menu-Choose --## Holds choosen Network Interface Index to work with            
    $networkIndex = Menu-Choose $strippedNetworkIndex $networkChooseTitle
    #$networkIndex[0]  ### Debug -

    $resetAdapter = $FAIL_RESULT
    $modificationCatagoryChoosen = $null
    $modificationChoosen = $null
    $modificationOptionChoosen = $null
    $DCBmenuItems2 = $null
    $DCBMenuTitle2 = $null
    $DCBmenuItems3 = $null
    $DCBMenuTitle3 = $null

    Start-Sleep -s .7

    If ($result = $PASS_RESULT) {
    ## Find Current Config in order to display it to user
        $dcbConfig = Find-Config $networkIndex
    }

    #The following 'DO WHILEs' are for the "Go back to previous Menu" functionality.
    Do {
        Do {

            ## Function Menu-Choose --## Let user choose which catagory of modification to perform
            $modificationCatagoryChoosen = Menu-Choose $DCBmenuItems $DCBMenuTitle $networkIndex -scope global
            If (($DCBmenuItems.count - 1) -eq $modificationCatagoryChoosen) {
                $resetAdapter = $PASS_RESULT
                break
            }

            # These If Then statements allow reset of adapter without changing settings

            $DCBmenuItems2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Option
            $DCBMenuTitle2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Name

            Start-Sleep -s .7

            ## Function Menu-Choose --## Let user choose which modification to perform
            $modificationChoosen = Menu-Choose $DCBmenuItems2 $DCBMenuTitle2 $networkIndex


        } While (($modificationChoosen -eq $DCBmenuItems2.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT))

        If (($DCBmenuItems.count - 1) -eq $modificationCatagoryChoosen) {
                break
        }
        # These If Then statements allow reset of adapter without changing settings

        ## Changes the options to choose on Menu-Choose to last chosen catagory
        $DCBmenuItems3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Option
        $DCBMenuTitle3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Name

        Start-Sleep -s .7

        ## Function Menu-Choose --## Let user choose how to modify DCB setting
        $modificationOptionChoosen = Menu-Choose $DCBmenuItems3 $DCBMenuTitle3 $networkIndex

    } While (($modificationOptionChoosen -eq $DCBmenuItems3.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT)) 

    # These If Then statements allow reset of adapter without changing settings
    If ($resetAdapter -eq $FAIL_RESULT){
        Start-Sleep -s .7

         ## Function Set-RegistryValues --## Records modified DCB setting to registry
        Set-RegistryValues $xmlDCBregEdits $modificationCatagoryChoosen $modificationChoosen $modificationOptionChoosen
    }

Return $networkIndex

}

IkeaTheSofa
  • 63
  • 1
  • 1
  • 6
0

The location of this error doesn't seem right. I don't see how PowerShell is getting that type of error on the if () condition. However this line could be causing the error:

$DCBmenuItems3.GetUpperBound(0)

Add a check to make sure $DCBmenuItems3 is not null. Also, when debugging issues (or heck just as a normal practice) I put Set-StrictMode -version latest at the top of my scripts.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thanks for the answer Keith. I'll check into $DCBmenuItem3 and make sure it's not null. I'll also try `Set-StrictMode -version latest` and see if that illuminates anything. The error was perplexing to me as well, I thought it might be in another part of the code. I'll let you know if this helps. Thanks again. – IkeaTheSofa Sep 07 '12 at 23:04
  • I added `Set-StrictMode -version latest` and though it threw a couple other unexpected errors, after fixing those it still errors out on the If () condition. Weird. – IkeaTheSofa Sep 10 '12 at 20:18
0
FAIL_RESULT = 0
PASS_RESULT = 1

typo ?

$FAIL_RESULT = 0
$PASS_RESULT = 1
walid2mi
  • 2,704
  • 15
  • 15
  • Yes it was a typo. In my script I declare these this way: `## Value a function will return is it passes Set-Variable PASS_RESULT -option Constant -value 1 -ea SilentlyContinue ## Value a function will return is it passes Set-Variable FAIL_RESULT -option Constant -value 0 -ea SilentlyContinue` When I wrote them into this example I forgot the $. – IkeaTheSofa Sep 10 '12 at 20:15