question

ChristopherJack-1763 avatar image
0 Votes"
ChristopherJack-1763 asked ChristopherJack-1763 commented

Index into Null array

Hi,

I have the following powershell code

    $Headeroutput =  @(); 
    
     for ($x=0; $x -le $Headerinfo.Count-1; $x++)
     {
         $x
         $Headerinfo[$x]."CustomerOrderNumber"
    
         $Headeroutput  += $Headerinfo[$x]."CustomerOrderNumber",$Headerinfo[$x]."Type",$Headerinfo[$x]."one",$Headerinfo[$x]."two",$Headerinfo[$x]."three",$Headerinfo[$x]."four",$Headerinfo[$x]."Status",$Headerinfo[$x]."five",$Headerinfo[$x]."six",$Headerinfo[$x]."seven",$Headerinfo[$x]."eight",$Headerinfo[$x]."customer_account",$Headerinfo[$x]."nine",$Headerinfo[$x]."ten",$heHeaderinfoader[$x]."eleven"
         $result += $Headeroutput -join "," 

I am getting the error


Line |
99 | $Headeroutput += $Headerinfo[$x]."CustomerOrderNumber",$Head …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot index into a null array.

I am not really understanding as I am initialising the array.

Any help appreciated.




windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ChristopherJack-1763 avatar image
0 Votes"
ChristopherJack-1763 answered ChristopherJack-1763 commented

Solved .. was a typo at the end.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When you're do repetitive stuff like that it's much better to line them up. It makes spotting those "oops!" moment easier.

 $Headeroutput  +=   $Headerinfo[$x]."CustomerOrderNumber",
                     $Headerinfo[$x]."Type",
                     $Headerinfo[$x]."one",
                     $Headerinfo[$x]."two",
                     $Headerinfo[$x]."three",
                     $Headerinfo[$x]."four",
                     $Headerinfo[$x]."Status",
                     $Headerinfo[$x]."five",
                     $Headerinfo[$x]."six",
                     $Headerinfo[$x]."seven",
                     $Headerinfo[$x]."eight",
                     $Headerinfo[$x]."customer_account",
                     $Headerinfo[$x]."nine",
                     $Headerinfo[$x]."ten",
                     $heHeaderinfoader[$x]."eleven"
1 Vote 1 ·

Or just move all the "constant" stuff out of the loop altogether. That makes what you're doing easier to understand, and if you have to change a property name you only have to alter the value in the array instead of taking a chance on messing up the code. For example:

 $props =    "CustomerOrderNumber",
             "Type",
             "one",
             "two",
             "three",
             "four",
             "Status",
             "five",
             "six",
             "seven",
             "eight",
             "customer_account"
             "nine",
             "ten",
             "eleven"
    
 0..($Headerinfo.Count - 1) |
     ForEach-Object{
         $x = $_
         0..($props.count - 1) |
             ForEach-Object{
                 $Headeroutput += $Headerinfo[$x].$props[$_]
     }
1 Vote 1 ·

Thanks Rich, Some good ideas, ill take them on board.

0 Votes 0 ·