Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot index into a null array

I am using a template that sets the cell color based on a condition if a server backup was successful or not.

I have the below code which is continuously throwing the error: Cannot index into a null array:

Cannot index into a null array.
At C:\Users\admin\Desktop\new.html.ps1:65 char:17
+ ...             $Value = $Search.Matches[$Index].Groups[1].Value -as [dou ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

The final output still produces the correct results (colors etc), but how can I set the script to ignore any null values so I no longer receive these errors? I have tested with condition 'if ($Value -ne $null)' which works well, however no colors are shown on the cells.

Code:

Process {
    foreach ($Line in $InputObject) {
        if ($Line.IndexOf("<tr><th") -ge 0) {
            Write-Verbose "$(Get-Date): Processing headers..."
            $Search = $Line | Select-String -Pattern '<th ?[a-z\-:;"=]*>(.*?)<\/th>' -AllMatches
            $Index = 0
            foreach ($Match in $Search.Matches) {
                if ($Match.Groups[1].Value -eq $Property) {
                    break
                }
                $Index ++
            }
            if ($Index -eq $Search.Matches.Count) {
                Write-Warning "$(Get-Date): Unable to locate property: $Property in table header"
                exit
            }
            Write-Verbose "$(Get-Date): $Property column found at index: $Index"
        }

        if ($Line -match "<tr( style=""background-color:.+?"")?><td") {
            $Search = $Line | Select-String -Pattern '<td ?[a-z\-:;"=]*>(.*?)<\/td>' -AllMatches
            $Value = $Search.Matches[$Index].Groups[1].Value -as [double]
            if (-not $Value) {
                $Value = $Search.Matches[$Index].Groups[1].Value
            }
            if (Invoke-Command $Filter) {
                if ($Row) {
                    Write-Verbose "$(Get-Date): Criteria met!  Changing row to $Color..."
                    if ($Line -match "<tr style=""background-color:(.+?)"">") {
                        $Line = $Line -replace "<tr style=""background-color:$($Matches[1])","<tr style=""background-color:$Color"
                    } else {
                        $Line = $Line.Replace("<tr>","<tr style=""background-color:$Color"">")
                    }
                } else {
                    Write-Verbose "$(Get-Date): Criteria met!  Changing cell to $Color..."
                    $Line = $Line.Replace($Search.Matches[$Index].Value,"<td style=""background-color:$Color"">$Value</td>")
                }
            }
        }         
        Write-Output $Line
    }
}     
End {
    Write-Verbose "$(Get-Date): Function Set-CellColor completed"
}
like image 793
Vulgarito Avatar asked Jan 05 '18 00:01

Vulgarito


1 Answers

The issue here is that you're trying to index into an array that has zero length (nothing in it at all) during one of the loops. I assume this is because one of the lines being processed has no matches at all.

You could use something to check if there are any matches at all before trying to access the index.

Something like..

if ($Search.Matches.Count -gt 0) {

}

If you want to suppress the errors, then you can use something like

-errorAction SilentlyContinue

to suppress the errors.

like image 169
thatguyfig Avatar answered Nov 09 '22 21:11

thatguyfig