Different behavior when looping through a list of variables 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...
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 SectionModelthus your code as it is currently is not really performant