/errors/powershell/you-cannot-call-a-method-on-a-null-valued-expression
PowerShell: You cannot call a method on a null-valued expression.
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
- tool
- PowerShell
- exit code
- 1
- versions
- Windows PowerShell 5.1, PowerShell 7.x
- os
- windows, linux, macos
You cannot call a method on a null-valued expression. means the part before .Method() evaluated to $null. PowerShell does not say which part: in $config.Database.Name.ToUpper(), any of $config, .Database or .Name can be the null one. Print the pieces, find the first $null, and fix the lookup that produced it, or guard the call with if ($null -ne $x).
Reproduce it
$ErrorActionPreference = 'Stop'
$config = [pscustomobject]@{ Database = [pscustomobject]@{ Host = 'db01' } }
try { $config.Database.Name.ToUpper() } catch { $_.Exception.Message; ' -> ' + $_.FullyQualifiedErrorId }
"Host: [$($config.Database.Host)] Name: [$($config.Database.Name)] Name is null: $($null -eq $config.Database.Name)"
$file = Get-ChildItem -Filter *.nomatch | Select-Object -First 1
try { $file.Name.Replace('a', 'b') } catch { $_.Exception.Message }Output
You cannot call a method on a null-valued expression. -> InvokeMethodOnNull Host: [db01] Name: [] Name is null: True You cannot call a method on a null-valued expression.
Reading a property that does not exist returns $null without an error; only the method call fails.
Guard the call
$name = $null
if ($null -ne $name) { $name.ToUpper() } else { 'name is empty' }
"[$("$name".ToUpper())]"
Set-StrictMode -Version Latest
$config = [pscustomobject]@{ Host = 'db01' }
try { $config.Name } catch { $_.Exception.Message }Output
name is empty [] The property 'Name' cannot be found on this object. Verify that the property exists.
- Put
$nullon the left of comparisons ($null -ne $x): with an array on the left,-nefilters the array instead of testing it. "$value"turns$nullinto an empty string, which is safe when an empty result is acceptable.- Strict mode makes missing properties fail at the property access, which points to the real cause.
- PowerShell 7.1 and later have null-conditional member access,
${name}?.ToUpper(), which returns$nullinstead of failing. Windows PowerShell 5.1 does not have it.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators
Related pages
errors · powershellPowerShell: Cannot index into a null array.You indexed a variable that is $null: a command returned nothing, the name is misspelled or a property is missing. Check the value first or wrap it in @().errors · powershellPowerShell: Cannot bind argument to parameter 'Path' because it is null.A variable passed to a mandatory parameter is $null or empty: a typo in its name, a lookup that found nothing, or a script parameter that was never passed.flags · powershellPowerShell -ErrorAction Stop: why try/catch does not catch the errortry/catch only catches terminating errors, and most cmdlet errors are not. Add -ErrorAction Stop, or set $ErrorActionPreference to Stop, to catch them.