<p>You can just test if image is defined first using:</p>

<pre><code>{% set image = block.image.first() %}
{% if image %}
    &lt;a href="{{ image.getUrl() }}"&gt;...&lt;/a&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-12-15T00:55:13.810Z
2017-07-23T17:06:24.697Z
2017-07-23T17:06:24.697Z
Douglas McDonald
<p>This was working fine previously and not sure what triggered the error. Any idea how to resolve issue?</p>

<pre><code>{% for block in entry.servicesItems.type('images') %}
    &lt;div class="col"&gt;

        {# grab the first image #}
        {% set image = block.image.first() %}

        {# link to the full sized version #}
            &lt;a href="{{ image.getUrl }}"&gt;
        {# generated the transformed thumbnail version #}
            &lt;img src="{{ image.getUrl('servicesImageCrop')}}" alt="{{ block.alt }}" width="{{ image.getWidth('servicesImageCrop') }}" height="{{ image.getHeight('servicesImageCrop') }}"&gt;
            &lt;div id="lightbox-title"&gt;
                &lt;h2&gt;{{ block.imageTitle }}&lt;/h2&gt;
                &lt;p&gt;{{ block.imageContent }}&lt;/p&gt;
            &lt;/div&gt;
        &lt;/a&gt;
    &lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-11-13T21:25:21.337Z
Figured it out, it was actually caused by an empty matrix block in an entry. Any idea how to prevent this from page error if user accidentally does this?
frshjb373
CC BY-SA 3.0
2014-11-13T21:56:35.647Z
You can check if it is defined first using `{% if block.image.first() %}...{% endif %}` or `{% if image %}...{% endif %}`.
Douglas McDonald
CC BY-SA 3.0
2014-11-13T23:28:05.510Z
Can you add that as an official answer, @frshjb373?
Brad Bell
CC BY-SA 3.0
2014-11-15T18:18:51.817Z
If you found an answer to your question, you can post it :) Answering own questions is not discouraged.
nicael
<templating><matrix><image>
Getting error in template: Impossible to access an attribute ("getUrl") on a NULL variable ("")
# Getting error in template: Impossible to access an attribute ("getUrl") on a NULL variable ("")

(Asked by: username_0 on 2014-11-13)

This was working fine previously and not sure what triggered the error. Any idea how to resolve issue?
{% for block in entry.servicesItems.type('images') %}
    

        {# grab the first image #}
        {% set image = block.image.first() %}

        {# link to the full sized version #}
            
        {# generated the transformed thumbnail version #}
            
            
                {{ block.imageTitle }}
                {{ block.imageContent }}
            
        
    
{% endfor %}

**Tags:** templating, matrix, image

> **Comments:**
> **username_0**: Figured it out, it was actually caused by an empty matrix block in an entry. Any idea how to prevent this from page error if user accidentally does this?  
> **username_1**: You can check if it is defined first using `{% if block.image.first() %}...{% endif %}` or `{% if image %}...{% endif %}`.  
> **username_2**: Can you add that as an official answer, @username_0?  
> **username_3**: If you found an answer to your question, you can post it :) Answering own questions is not discouraged.  

---

## 1 answers

### Answer by username_1 on 2014-12-15. Score: 2 | Accepted answer

You can just test if image is defined first using:
{% set image = block.image.first() %}
{% if image %}
    ...
{% endif %}


---

