<p>Make sure your <code>section.first()</code> returns an element. The error occurs because <code>section.first()</code> is <code>null</code> => no element was found with your criteria. And the value <code>null</code> has no function <code>getSection</code>.</p>

<p>However - keep in mind your <code>sectionHandle</code> variable is not the handle of the section, but the <code>SectionModel</code>thus your code as it is currently is not really performant</p>

CC BY-SA 3.0
2018-04-14T21:08:22.193Z
2018-04-14T21:13:39.560Z
2018-04-14T21:13:39.560Z
Robin Schambach
<p>I apologize for the seemingly vague title, but I honestly don't know the best way to state this problem succinctly!</p>

<p>This is within the context of a particular section's entry template.</p>

<p><strong>What I want to happen:</strong></p>

<p>I want to loop through a list of variables that call different Entry elementCriteriaModels and for each iteration of this loop I want to get the section's handle.</p>

<p><strong>How I tried to make it happen:</strong></p>

<p><em>Get related entries from a number of different sections</em></p>

<pre><code>{% set towns = craft.entries.section('towns').relatedTo(county) %}
{% set citizens = craft.entries.section('inductees').relatedTo(county) %}
{% set events = craft.entries.section('events').relatedTo(county) %}
</code></pre>

<p><em>Make a list of those elementCriteriaModel objects</em></p>

<pre><code>{% set arrayOfSections = [
    towns,
    citizens,
    events
] %}
</code></pre>

<p><em>Loop through that list and try to get the section handle of whatever the first entry is of each section</em></p>

<pre><code>{% for section in arrayOfSections %}
    {% set sectionHandle = section.first().getSection() %}
    {{ sectionHandle }}
</code></pre>

<p><strong>What actually happens</strong></p>

<p>I get the following error: "Impossible to invoke a method ("getSection") on a null variable."</p>

<p>When I try to do the same thing on an object that is outside of that <code>arrayOfSections</code> list, it works just fine!</p>

<p><strong>Why am I even trying to do this??</strong></p>

<p>There is a huge part of this template that repeats itself over and over. If I can successfully loop through these related sections in this way that problem will be fixed. I've thought about turning this into a macro, but I'd still have to call this macro nearly half a dozen times and manually feed in the desired section each time...</p>

<templating>
Different behavior when looping through a list of variables
# Different behavior when looping through a list of variables

(Asked by: username_0 on 2018-04-14)

I apologize for the seemingly vague title, but I honestly don't know the best way to state this problem succinctly!
This is within the context of a particular section's entry template.
What I want to happen:
I want to loop through a list of variables that call different Entry elementCriteriaModels and for each iteration of this loop I want to get the section's handle.
How I tried to make it happen:
Get related entries from a number of different sections
{% set towns = craft.entries.section('towns').relatedTo(county) %}
{% set citizens = craft.entries.section('inductees').relatedTo(county) %}
{% set events = craft.entries.section('events').relatedTo(county) %}

Make a list of those elementCriteriaModel objects
{% set arrayOfSections = [
    towns,
    citizens,
    events
] %}

Loop through that list and try to get the section handle of whatever the first entry is of each section
{% for section in arrayOfSections %}
    {% set sectionHandle = section.first().getSection() %}
    {{ sectionHandle }}

What actually happens
I get the following error: "Impossible to invoke a method ("getSection") on a null variable."
When I try to do the same thing on an object that is outside of that arrayOfSections list, it works just fine!
Why am I even trying to do this??
There is a huge part of this template that repeats itself over and over. If I can successfully loop through these related sections in this way that problem will be fixed. I've thought about turning this into a macro, but I'd still have to call this macro nearly half a dozen times and manually feed in the desired section each time...

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2018-04-14. Score: 1

Make sure your `section.first()` returns an element. The error occurs because `section.first()` is `null` => no element was found with your criteria. And the value `null` has no function `getSection`.

However - keep in mind your `sectionHandle` variable is not the handle of the section, but the `SectionModel`thus your code as it is currently is not really performant


---

