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

**URL:** <https://forums.powershell.org/t/cannot-index-in-to-a-null-array-error-using-2-arrays/14800>\
**Category:** PowerShell Help\
**Created:** [August 14, 2020, 6:22am UTC](https://forums.powershell.org/t/cannot-index-in-to-a-null-array-error-using-2-arrays/14800 "2020-08-14T06:22:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgrate2112](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@dgrate2112](https://forums.powershell.org/u/dgrate2112)\
**Post date:** [August 14, 2020, 6:22am UTC](https://forums.powershell.org/t/cannot-index-in-to-a-null-array-error-using-2-arrays/14800/1 "2020-08-14T06:22:26Z")

</div>

**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

&nbsp;

**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]
- 

```auto

```

- CategoryInfo : InvalidOperation: (🙂 , RuntimeException
- FullyQualifiedErrorId : NullArray

&nbsp;

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.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [August 16, 2020, 9:56am UTC](https://forums.powershell.org/t/cannot-index-in-to-a-null-array-error-using-2-arrays/14800/2 "2020-08-16T09:56:53Z")

</div>

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

```
$Primary[0].($MergeColHeaders[1])
```

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

```
If($MergeColHeaders[1]){
   $Primary[0].($MergeColHeaders[1])
}
```

---

<div class="post-metadata">

**Author:** ![dgrate2112](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@dgrate2112](https://forums.powershell.org/u/dgrate2112)\
**Post date:** [August 19, 2020, 3:28am UTC](https://forums.powershell.org/t/cannot-index-in-to-a-null-array-error-using-2-arrays/14800/3 "2020-08-19T03:28:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/cannot-index-in-to-a-null-array-error-using-2-arrays/14800/4 "2024-05-16T20:29:00Z")

</div>


