# How to properly set up foreach and empty list

**URL:** <https://community.linx.software/community/t/how-to-properly-set-up-foreach-and-empty-list/677>\
**Category:** Support\
**Created:** [January 27, 2024, 2:05am UTC](https://community.linx.software/community/t/how-to-properly-set-up-foreach-and-empty-list/677 "2024-01-27T02:05:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferula](https://community.linx.software/community/user_avatar/community.linx.software/ferula/32/1416_2.png) [@Ferula](https://community.linx.software/community/u/Ferula)\
**Post date:** [January 27, 2024, 2:05am UTC](https://community.linx.software/community/t/how-to-properly-set-up-foreach-and-empty-list/677/1 "2024-01-27T02:05:43Z")

</div>

I have a list parameter that Comes into my function. This list contains the three separate variables or strings. of those variables I use the string builder to make a complete statement and then I’m trying to assign to another list of the completed statements. I’m starting from an empty list though. I keep getting the error can’ add to null list.

Error Image  
 ![image](https://community.linx.software/community/uploads/default/original/2X/2/215cc6886c087e721ef5fda8b7f063ce83b432f8.png)

Solution Image

 ![image](https://community.linx.software/community/uploads/default/original/2X/0/09e0d301c454f6e53638c1e6f60ff23b9fc921fa.png)

Values Debug

 ![image](https://community.linx.software/community/uploads/default/original/2X/1/1f629901517d3e8b525b4046a94dbd1913024fdf.png)  
Yellow highlight is the list i’m trying to use the add to list function on.  
Purple highlight is the value i’m using as the souce value for add to ilst.  
Parts is of the same type as geminicontentpart

---

<div class="post-metadata">

**Author:** ![gawie](https://community.linx.software/community/user_avatar/community.linx.software/gawie/32/185_2.png) [@gawie](https://community.linx.software/community/u/gawie)\
**Post date:** [January 29, 2024, 9:43am UTC](https://community.linx.software/community/t/how-to-properly-set-up-foreach-and-empty-list/677/2 "2024-01-29T09:43:52Z")

</div>

It looks like you’re trying to add to the list in GeminiContent.parts, but the list is null. Initialize the list to an empty list when creating GeminiContent. It looks like this:

![solution](https://community.linx.software/community/uploads/default/original/2X/1/13824f98280c6f1080a3a3fff3bc4bbd49bf56f6.png)

The parts property is initialized to (an empty list).

An alternative is to create a separate instance of a list, add to the list, and assign the list to the parts property.

When Linx creates a top level list, it defaults to an empty list. This function will work:

![basic](https://community.linx.software/community/uploads/default/original/2X/3/36a223d435247dc3c770d4b9a8edb76070afd1d3.png)

When Linx creates an instance of a custom Type, it defaults to an instance of the type but properties of the type are not initialized. In the below example, Person has a List property called Names. AddToList references Person.Names. Person.Names defaults to null, so this function will not work:

![fail](https://community.linx.software/community/uploads/default/original/2X/1/169a657271bb6f2bb182fbf672f90073513e1e9a.png)

---

<div class="post-metadata">

**Author:** ![Ferula](https://community.linx.software/community/user_avatar/community.linx.software/ferula/32/1416_2.png) [@Ferula](https://community.linx.software/community/u/Ferula)\
**Post date:** [January 29, 2024, 10:47pm UTC](https://community.linx.software/community/t/how-to-properly-set-up-foreach-and-empty-list/677/3 "2024-01-29T22:47:46Z")

</div>

Gotcha, Thanks so much! Might help to add a section to the relevant documentation pages mentioning this.
