您不能在空值表达式上调用方法 - 一般
You cannot call a method on a null-valued expression - general
您创建了一个脚本,它运行了一段时间,然后突然开始崩溃并显示 "You cannot call a method on a null-valued expression" 或 "The property 'property name' cannot be found on this object. Verify that the property exists and can be set."。这是什么意思?
这是 "null pointer exception" 的 Powershell 版本。每次您尝试查询看似为空的变量时都会出现此异常。要确定哪个变量为 null 以及在哪里,您需要阅读堆栈跟踪和相关行的 line/symbol 编号。一个例子:
You cannot call a method on a null-valued expression.
At E:\temp\testsest.ps1:35 char:12
+ If($Search.value() -contains $SearchString)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
让我们分析错误信息。首先,这个问题的标题中有一个措辞。如果你要用这种措辞提出问题,你会得到一组由 Whosebug 提出的类似问题。但是错误描述中还有更多内容。第二行显示生成此异常的表达式的第一个字符的脚本、行和字符编号。在这里,向 $Search.value()
发出请求,查询是否 -contains $SearchString
。波浪形下划线将表达式完全分隔开,尽管正确的方法是仅使用下划线 $Search.value()
。接下来,有一个CategoryInfo
和FullyQualifiedErrorId
,后者表示"Invoke method on null",省略了"pointer"或"variable"。
现在,让我们调试消息。在这里,唯一要调用的方法是 value()
,这意味着 $Search
等于 null。因此,我们需要从脚本的第 35 行向上找到一个值最后被赋值给相关变量的地方。此特定脚本对 Range.Find()
进行查询,如果与搜索到的字符串不匹配,则 return 为 null。摘录:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath)
$ExcelWorkSheet = $Excel.WorkSheets.item("$location")
$Range = $ExcelWorkSheet.Range("A1").EntireColumn
$Search = $Range.find($user) # <<< here we get null
If($Search.value() -contains $user)
所以,我们找到了我们从哪里收到一个空值。
补救措施各不相同,但都包括针对 $null
的检查。在这种情况下,检查 $Search
是否为 null 以及 return "Nothing found" 是否确实为 null 就足够了。它可能不那么简单,可能有更多结构可以为空,例如 $a.b.c.someMethod()
- 这里 $a
、$a.b
或 $a.b.c
为空,因此您需要检查所有结果。也有这样的情况,一个复杂的结构被 returned,并期望在某个字段中有一个值,但是该字段没有被填充,因此尝试使用该字段的值将产生一个异常。
原则是:如果您收到有关 "null-valued" 的异常,您没有预料到 return 为 null,您必须添加对 null 的检查(或者,实际上,任何意外的)值,然后再尝试使用结果。
您创建了一个脚本,它运行了一段时间,然后突然开始崩溃并显示 "You cannot call a method on a null-valued expression" 或 "The property 'property name' cannot be found on this object. Verify that the property exists and can be set."。这是什么意思?
这是 "null pointer exception" 的 Powershell 版本。每次您尝试查询看似为空的变量时都会出现此异常。要确定哪个变量为 null 以及在哪里,您需要阅读堆栈跟踪和相关行的 line/symbol 编号。一个例子:
You cannot call a method on a null-valued expression.
At E:\temp\testsest.ps1:35 char:12
+ If($Search.value() -contains $SearchString)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
让我们分析错误信息。首先,这个问题的标题中有一个措辞。如果你要用这种措辞提出问题,你会得到一组由 Whosebug 提出的类似问题。但是错误描述中还有更多内容。第二行显示生成此异常的表达式的第一个字符的脚本、行和字符编号。在这里,向 $Search.value()
发出请求,查询是否 -contains $SearchString
。波浪形下划线将表达式完全分隔开,尽管正确的方法是仅使用下划线 $Search.value()
。接下来,有一个CategoryInfo
和FullyQualifiedErrorId
,后者表示"Invoke method on null",省略了"pointer"或"variable"。
现在,让我们调试消息。在这里,唯一要调用的方法是 value()
,这意味着 $Search
等于 null。因此,我们需要从脚本的第 35 行向上找到一个值最后被赋值给相关变量的地方。此特定脚本对 Range.Find()
进行查询,如果与搜索到的字符串不匹配,则 return 为 null。摘录:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath)
$ExcelWorkSheet = $Excel.WorkSheets.item("$location")
$Range = $ExcelWorkSheet.Range("A1").EntireColumn
$Search = $Range.find($user) # <<< here we get null
If($Search.value() -contains $user)
所以,我们找到了我们从哪里收到一个空值。
补救措施各不相同,但都包括针对 $null
的检查。在这种情况下,检查 $Search
是否为 null 以及 return "Nothing found" 是否确实为 null 就足够了。它可能不那么简单,可能有更多结构可以为空,例如 $a.b.c.someMethod()
- 这里 $a
、$a.b
或 $a.b.c
为空,因此您需要检查所有结果。也有这样的情况,一个复杂的结构被 returned,并期望在某个字段中有一个值,但是该字段没有被填充,因此尝试使用该字段的值将产生一个异常。
原则是:如果您收到有关 "null-valued" 的异常,您没有预料到 return 为 null,您必须添加对 null 的检查(或者,实际上,任何意外的)值,然后再尝试使用结果。