<p>Ok, so it came out that the post I used for testing wasn't assigned to any category and that was causing the exception. </p>

<p>I added a validation for checking if the category is defined:</p>

<pre><code>{{ (entry.blogCategory.one().id is defined and entry.blogCategory.one().id is not null) and entry.blogCategory.one().id == category.id ? 'light': 'primary' }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
</code></pre>

CC BY-SA 4.0
2019-09-17T15:33:40.017Z
2019-09-17T15:33:40.017Z
krzk
<p>I'm trying to upgrade my website to Craft 3 and can't resolve one issue with this piece of code:</p>

<pre><code>{% for category in craft.categories.group('blogCategories').order('title') %}
   &lt;li&gt;&lt;a href="{{ category.url }}" class="button {{ entry.blogCategory.one().id == category.id ? 'light': 'primary' }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}
</code></pre>

<p>I get the following error: </p>

<blockquote>
  <p>Twig Runtime Error – Twig\Error\RuntimeError
  Impossible to access an attribute ("id") on a null variable.</p>
</blockquote>

<p>I can't find any information in <em>Changes in Craft 3</em>, but I bet there's a new way of getting the category ID of the current post and the <code>entry.blogCategory.one().id</code> won't work anymore? </p>

<p>Any ideas? </p>

<templating><craft3>
Can't get current entry's ID - Impossible to access an attribute ("id") on a null variable
# Can't get current entry's ID - Impossible to access an attribute ("id") on a null variable

(Asked by: username_0 on 2019-09-17)

I'm trying to upgrade my website to Craft 3 and can't resolve one issue with this piece of code:
{% for category in craft.categories.group('blogCategories').order('title') %}
   {{ category.title }}
{% endfor %}

I get the following error: 

Twig Runtime Error – Twig\Error\RuntimeError
  Impossible to access an attribute ("id") on a null variable.

I can't find any information in Changes in Craft 3, but I bet there's a new way of getting the category ID of the current post and the entry.blogCategory.one().id won't work anymore? 
Any ideas?

**Tags:** templating, craft3


---

## 1 answers

### Answer by username_0 on 2019-09-17. Score: 1 | Accepted answer

Ok, so it came out that the post I used for testing wasn't assigned to any category and that was causing the exception. 
I added a validation for checking if the category is defined:
{{ (entry.blogCategory.one().id is defined and entry.blogCategory.one().id is not null) and entry.blogCategory.one().id == category.id ? 'light': 'primary' }}">{{ category.title }}


---

