Reputation: 63
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
Upvotes: 0
Views: 14054
Reputation: 63
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
}
Upvotes: 1
Reputation: 2888
FAIL_RESULT = 0
PASS_RESULT = 1
typo ?
$FAIL_RESULT = 0
$PASS_RESULT = 1
Upvotes: 0
Reputation: 201602
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.
Upvotes: 0