<p>The error tells you that <code>faqs</code> is <code>null</code>. As you're using the <code>first()</code> function to execute your element query, this means that your query has no matches.</p>
<ol>
<li>Are you sure that the section handle (<code>FAQ</code>) is in uppercase?</li>
<li>The correct way to create an entry query in Craft 3 is <code>craft.entries()</code>. Although it may work without the parenthesis, it can't hurt to add them.</li>
<li>You can replace <code>.search('slug::' ~ entry.slug)</code>  with <code>.slug(entry.slug)</code>. It's easier to understand and should have the same effect.</li>
<li>Are you sure that the query always returned something in Craft 2? It may be worth adding a null check to avoid the error: <code>{% if faqs and faqs.questionsanswers and faqs.questionsanswers|length &gt; 0 %}</code></li>
</ol>

CC BY-SA 4.0
2020-09-15T18:10:29.170Z
Thanks a lot for your reply, your insight is very helpful and I have already used some of your suggestions to help better format the code.
crazymatt
CC BY-SA 4.0
2020-09-14T21:50:45.527Z
2020-09-14T21:50:45.527Z
nstCactus
<p>I am not super fluent in Twig and I inherited a website from a company that built the site in Craft v2. The site was upgraded to version 3 of Craft recently and one of the template pages is throwing an error where on version 2 of Craft it was not. Code:</p>
<pre><code>{% set faqs = craft.entries.section('FAQ').search('slug::' ~ entry.slug).first() %}

{% if faqs.questionsanswers|length &gt; 0 %}
  &lt;a href=&quot;#link&quot;&gt;FAQs&lt;/a&gt;
{% endif %}
</code></pre>
<p>This is throwing an error: <code>Impossible to access an attribute (&quot;questionsanswers&quot;) on a null variable.</code></p>
<p>So since it was returning <code>null</code> I changed the code to this which seems to be working:</p>
<pre><code>{% if faqs is not null and faqs is not empty %}
  &lt;a href=&quot;#link&quot;&gt;FAQs&lt;/a&gt;
{% endif %}
</code></pre>
<p>My question is why would this code not work for version 3 when it was working correctly for version 2? Why would the length return <code>null</code> instead of <code>0</code>? I did not do the upgrade but maybe something was missed? (This is a template page and some of the entries do have FAQs and some don't) Or perhaps something with the structure of the version 3 of Craft has changed? Lastly if there is a better way to do/code what is being done I would love to hear it.</p>

<templating><craft3>
Null Length Error on Craft Version 3
# Null Length Error on Craft Version 3

(Asked by: username_0 on 2020-09-09)

I am not super fluent in Twig and I inherited a website from a company that built the site in Craft v2. The site was upgraded to version 3 of Craft recently and one of the template pages is throwing an error where on version 2 of Craft it was not. Code:
{% set faqs = craft.entries.section('FAQ').search('slug::' ~ entry.slug).first() %}

{% if faqs.questionsanswers|length > 0 %}
  FAQs
{% endif %}

This is throwing an error: Impossible to access an attribute ("questionsanswers") on a null variable.
So since it was returning null I changed the code to this which seems to be working:
{% if faqs is not null and faqs is not empty %}
  FAQs
{% endif %}

My question is why would this code not work for version 3 when it was working correctly for version 2? Why would the length return null instead of 0? I did not do the upgrade but maybe something was missed? (This is a template page and some of the entries do have FAQs and some don't) Or perhaps something with the structure of the version 3 of Craft has changed? Lastly if there is a better way to do/code what is being done I would love to hear it.

**Tags:** templating, craft3


---

## 1 answers

### Answer by username_1 on 2020-09-14. Score: 2 | Accepted answer

The error tells you that `faqs` is `null`. As you're using the `first()` function to execute your element query, this means that your query has no matches.

1. Are you sure that the section handle (`FAQ`) is in uppercase?
2. The correct way to create an entry query in Craft 3 is `craft.entries()`. Although it may work without the parenthesis, it can't hurt to add them.
3. You can replace `.search('slug::' ~ entry.slug)` with `.slug(entry.slug)`. It's easier to understand and should have the same effect.
4. Are you sure that the query always returned something in Craft 2? It may be worth adding a null check to avoid the error: `{% if faqs and faqs.questionsanswers and faqs.questionsanswers|length > 0 %}`

> **Comments:**
> **username_0**: Thanks a lot for your reply, your insight is very helpful and I have already used some of your suggestions to help better format the code.  

---

