Cannot index in to a null array error… using 2 arrays??

Welcome Forums General PowerShell Q&A Cannot index in to a null array error… using 2 arrays??

Viewing 1 reply thread
  • Author
    Posts
    • #249611
      Participant
      Topics: 1
      Replies: 1
      Points: -4
      Rank: Member

      Determined a fix:

      Before referencing the index to an index… make it a string first:

      $c=$MergeColHeaders[$i]; write-host $c t:t $Update[0].$c

       

      Typing:

      PS C:\Projects\mySQL\HardWay> $Primary.gettype()

      IsPublic IsSerial Name BaseType
      ——– ——– —- ——–
      True True Object[] System.Array

      PS C:\Projects\mySQL\HardWay> $MergeColHeaders.gettype()

      IsPublic IsSerial Name BaseType
      ——– ——– —- ——–
      True True Object[] System.Array

      Hard-coded Test:

      PS C:\Projects\mySQL\HardWay> $MergeColHeaders[1]
      TRACKING

      PS C:\Projects\mySQL\HardWay> $Primary[0].tracking
      resp

      Desired Attempt:

      PS C:\Projects\mySQL\HardWay> $Primary[0].$MergeColHeaders[1]
      Cannot index into a null array.
      At line:1 char:1
      + $Primary[0].$MergeColHeaders[1]
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray

       

      I’m a “recovering programmer” and think my coding days are tainting my ability to understand PS.

      I feel like the above samples should prove ‘if A=B and B=C then A=C’ but, clearly, PS says otherwise.

      My assumption is I’m missing something obvious (which is usually my case!) but, I’ve googled for 1.5 days and have gotten no closer to unraveling this mystery.  This is part of a larger conundrum but, I think it starts here.

      Please let me know what else you may need from me.

      • This topic was modified 1 month ago by dgrate2112. Reason: RESOLVED-ish
    • #249911
      Senior Moderator
      Topics: 9
      Replies: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      That way won’t work as $MergeColHeaders is an array. it should be done like below

      But the safer way to always do id like below which is error proof.

      • #250850
        Participant
        Topics: 1
        Replies: 1
        Points: -4
        Rank: Member

        Thank you!  It’s always all about the syntax!!

Viewing 1 reply thread
  • You must be logged in to reply to this topic.