/errors/powershell/cannot-index-into-a-null-array
PowerShell: Cannot index into a null array.
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
Cannot index into a null array. means the expression before [...] is $null. PowerShell lets you read $null and properties of $null without error, but indexing it fails. Find out why the variable is empty: usually a command that found nothing, or a typo in the variable or property name. Wrapping the result in @() makes it an empty array, so [0] returns $null instead of failing.
Reproduce it
$ErrorActionPreference = 'Stop'
$files = Get-ChildItem -Filter *.nomatch
try { $files[0] } catch { $_.Exception.Message; ' -> ' + $_.FullyQualifiedErrorId }
"@() result count: $(@(Get-ChildItem -Filter *.nomatch).Count)"
"first of @(): [$(@(Get-ChildItem -Filter *.nomatch)[0])]"
'a.txt', 'b.txt' | ForEach-Object { Set-Content $_ 'x' }
(Get-ChildItem -Filter *.txt | Sort-Object Name)[0].NameOutput
Cannot index into a null array. -> NullArray @() result count: 0 first of @(): [] a.txt
One result is not an array either
A command that returns one object returns the object itself, not a one-element array. $result[0] then indexes into that object (a string's first character, for example) instead of returning it. @() fixes both cases:
'only.log' | ForEach-Object { Set-Content $_ 'x' }
$one = Get-ChildItem -Filter *.log
"type without @(): $($one.GetType().Name)"
"type with @(): $(@(Get-ChildItem -Filter *.log).GetType().Name)"
"string index: $('hello'[0])"Output
type without @(): FileInfo type with @(): Object[] string index: h
Safer patterns
| Instead of | Use |
|---|---|
| $x = Get-Thing; $x[0] | $x = @(Get-Thing); if ($x.Count) { $x[0] } |
| (Get-ChildItem *.log)[0] | Get-ChildItem *.log | Select-Object -First 1 |
| $text -match 'id=(\d+)'; $Matches[1] | if ($text -match 'id=(\d+)') { $Matches[1] } |
| $hash[$key] on an undefined $hash | $hash = @{} first |
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays
Related pages
errors · powershellPowerShell: You cannot call a method on a null-valued expression.You called a method on a value that is $null: a misspelled variable, a property that does not exist or a command that returned nothing. Check it first.compare · powershellWhere-Object vs .Where(): filtering collections in PowerShellWhere-Object filters a pipeline item by item; .Where() filters a collection already in memory, is faster, and has First, Last, SkipUntil and Split modes.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.